package cn.stylefeng.guns.modular.storage.service.impl;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.annotation.Resource;

import cn.stylefeng.guns.modular.order.model.params.WhOutOrderAllocateParam;
import cn.stylefeng.guns.modular.order.model.result.WhOutOrderAllocateResult;
import cn.stylefeng.guns.modular.order.service.WhOutOrderAllocateService;
import cn.stylefeng.guns.modular.replenishment.service.ReplenishmentOrderService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.core.util.ExcelWrite;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.entity.BasicGoods;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicGoodsService;
import cn.stylefeng.guns.modular.order.entity.OutOrder;
import cn.stylefeng.guns.modular.order.model.params.OutOrderDetailedParam;
import cn.stylefeng.guns.modular.order.model.params.OutOrderParam;
import cn.stylefeng.guns.modular.order.model.result.OutOrderDetailedResult;
import cn.stylefeng.guns.modular.order.service.OrderPutDetailedService;
import cn.stylefeng.guns.modular.order.service.OutOrderDetailedService;
import cn.stylefeng.guns.modular.order.service.OutOrderService;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.modular.storage.entity.StorageOutDetailed;
import cn.stylefeng.guns.modular.storage.entity.StorageStock;
import cn.stylefeng.guns.modular.storage.mapper.StorageOutDetailedMapper;
import cn.stylefeng.guns.modular.storage.model.params.StorageOutDetailedParam;
import cn.stylefeng.guns.modular.storage.model.params.StorageOutParam;
import cn.stylefeng.guns.modular.storage.model.params.StoragePutSingleParam;
import cn.stylefeng.guns.modular.storage.model.params.StorageStockParam;
import cn.stylefeng.guns.modular.storage.model.result.StorageOutDetailedResult;
import cn.stylefeng.guns.modular.storage.model.result.StorageOutResult;
import cn.stylefeng.guns.modular.storage.model.result.StoragePutSingleResult;
import cn.stylefeng.guns.modular.storage.model.result.StorageStockResult;
import cn.stylefeng.guns.modular.storage.service.StorageOutDetailedService;
import cn.stylefeng.guns.modular.storage.service.StorageOutService;
import cn.stylefeng.guns.modular.storage.service.StoragePutSingleService;
import cn.stylefeng.guns.modular.storage.service.StorageStockService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 拣货单明细表 服务实现类
 */
@Service
public class StorageOutDetailedServiceImpl extends ServiceImpl<StorageOutDetailedMapper, StorageOutDetailed> implements StorageOutDetailedService {

    @Resource
    private StorageOutService storageOutService;

    @Resource
    private OutOrderDetailedService outOrderDetailedService;

    @Resource
    private BasicGoodsService basicGoodsService;

    @Resource
    private BasicCustomerService basicCustomerService;

    @Resource
    private OrderPutDetailedService orderPutDetailedService;

    @Resource
    private StoragePutSingleService storagePutSingleService;

    @Resource
    private OutOrderService outOrderService;

    @Resource
    private StorageStockService storageStockService;

    @Resource
    private DictService dictService;

    @Resource
    private UserService userService;

    @Autowired
    private ReplenishmentOrderService replenishmentOrderService;
    @Autowired
    private WhOutOrderAllocateService outOrderAllocateService;

    private Lock lock = new ReentrantLock();

    @Override
    public void add(StorageOutDetailedParam param){
        StorageOutDetailed entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(StorageOutDetailedParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(StorageOutDetailedParam param){
        StorageOutDetailed oldEntity = getOldEntity(param);
        StorageOutDetailed newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StorageOutDetailedResult findBySpec(StorageOutDetailedParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<StorageOutDetailedResult> findListBySpec(StorageOutDetailedParam param){
        return this.baseMapper.findListBySpec(param);
    }

    @Override
    public List<StorageOutDetailedResult> findExportToExcelBySpec(StorageOutDetailedParam param,Long operateId){
        return this.baseMapper.findExportToExcelBySpec(param, operateId);
    }

    @Override
    public LayuiPageInfo findPageBySpec(StorageOutDetailedParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(StorageOutDetailedParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private StorageOutDetailed getOldEntity(StorageOutDetailedParam param) {
        return this.getById(getKey(param));
    }

    private StorageOutDetailed getEntity(StorageOutDetailedParam param) {
        StorageOutDetailed entity = new StorageOutDetailed();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class,timeout=60)
    public ResponseData checkStock(OutOrder outOrder,ResponseData data) {
        //新增拣货单明细
        OutOrderDetailedParam outOrderDetailedParam = new OutOrderDetailedParam();
        outOrderDetailedParam.setDetailedOutNumber(outOrder.getOutOrderNumber());
        List<OutOrderDetailedResult> orderDetailedResultList = outOrderDetailedService.findListBySpec(outOrderDetailedParam);
        for (OutOrderDetailedResult orderDetailedResult : orderDetailedResultList){
            StorageStockParam stockParam = new StorageStockParam();
            stockParam.setWarehouseId(outOrder.getOutStorehouseId());
            stockParam.setGoodsId(orderDetailedResult.getDetailedGoodsId());
            stockParam.setGoodsbatchs(orderDetailedResult.getDetailedBatchnumber());
            stockParam.setStockstate("IN_STOCK");
            StorageStockResult rStockResult = storageStockService.sumbysqzkk(stockParam);//查询该品 库存

            StorageOutDetailedParam detailedParam  = new StorageOutDetailedParam();
            detailedParam.setStorageGoodsId(orderDetailedResult.getDetailedGoodsId());
            detailedParam.setStorageBatchnumber(orderDetailedResult.getDetailedBatchnumber());
            StorageOutDetailedResult  StorageOutDetailedResult = findSumBySpec(detailedParam, outOrder.getOutStorehouseId());//查询待拣货 库存
            BigDecimal onum = new BigDecimal(0);//待拣库存
            if (StorageOutDetailedResult!=null) {
                onum = new BigDecimal(StorageOutDetailedResult.getStorageNumber());
            }
            BigDecimal Num = new BigDecimal(orderDetailedResult.getDetailedNumber()).add(onum);//出库库存
            if (rStockResult!=null) {
                BigDecimal num = new BigDecimal(rStockResult.getNum());
                if (Num.compareTo(num)==1) {
                    BigDecimal sub = Num.subtract(num);//系统库存   -   待拣库存 =  实际库存
                    data.setSuccess(false);
                    data.setMessage("Insufficient stock！"+outOrder.getCustomerNumber()+"<br>Product Code:"+orderDetailedResult.getDetailedGoodscode()+"<br>Product Name:"+orderDetailedResult.getDetailedGoodsname()+"<br>lack:"+sub.toString()+"quantity<br>batch:"+orderDetailedResult.getDetailedBatchnumber());
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return data;
                }
            } else {
                data.setSuccess(false);
                data.setMessage("Insufficient stock！"+outOrder.getCustomerNumber()+"<br>Product Code:"+orderDetailedResult.getDetailedGoodscode()+"<br>Product Name:"+orderDetailedResult.getDetailedGoodsname()+"<br>lack:"+Num.toString()+"quantity<br>batch:"+orderDetailedResult.getDetailedBatchnumber());
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return data;
            }
        }
        //更新出库订单
        outOrder.setOutOrderStatus("PENDING_CONFIRMATION");
        OutOrderParam param = new OutOrderParam();
        ToolUtil.copyProperties(outOrder,param);
        outOrderService.update(param);
        return ResponseData.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class,timeout=60)
    public ResponseData outSingle(OutOrder outOrder,ResponseData data) {
        StorageOutParam storageOutParam = new StorageOutParam();
        storageOutParam.setStorageOutNumber(outOrder.getCustomerNumber());
        //出库订单编号
        storageOutParam.setOutOrderNumber(outOrder.getOutOrderNumber());
        //库房id
        storageOutParam.setOutStorehouseId(outOrder.getOutStorehouseId());
        //库房名称
        storageOutParam.setStorehouseName(outOrder.getStorehouseName());
        //收货地址
        storageOutParam.setGetLocation(outOrder.getGetLocation());
        //收货地址id
        storageOutParam.setGetLocationId(outOrder.getGetLocationId());
        //收货单位
        storageOutParam.setGetUnit(outOrder.getGetUnit());
        //收货单位id
        storageOutParam.setGetUnitId(outOrder.getGetUnitId());
        //交付id
        storageOutParam.setDeliverId(outOrder.getDeliverId());
        storageOutParam.setDeptId(outOrder.getDeptId());
        storageOutParam.setOutOrderStatus("INCOMPLETE");

        storageOutParam.setSetMentiontime(outOrder.getSetMentiontime());//发货时间
        //不重复
        String uuidOne = UUID.randomUUID().toString().replaceAll("-","");
        storageOutParam.setRepetition(uuidOne);

        storageOutService.add(storageOutParam);

        //新增拣货单明细
        OutOrderDetailedParam outOrderDetailedParam = new OutOrderDetailedParam();
        outOrderDetailedParam.setDetailedOutNumber(outOrder.getOutOrderNumber());
        List<OutOrderDetailedResult> orderDetailedResultList = outOrderDetailedService.findListBySpec(outOrderDetailedParam);

        BasicCustomer basicCustomer = basicCustomerService.getById(outOrder.getOutCustomerId());
        List<StorageOutDetailed> solist2 = new ArrayList<StorageOutDetailed>();//要保存的 拣货单明细list
        for (OutOrderDetailedResult orderDetailedResult : orderDetailedResultList){
            StorageOutDetailed storage = prepareViewData(orderDetailedResult);
            storage.setSigns("A");
            storage.setStorageOutNumber(storageOutParam.getStorageOutNumber());//出库单
            storage.setStoragePutbalenum(orderDetailedResult.getDetailedNumber());//大包数
            storage.setStorageNumber(orderDetailedResult.getDetailedNumber());//数量

            StorageStockParam stockParam = new StorageStockParam();
            stockParam.setWarehouseId(outOrder.getOutStorehouseId());
            stockParam.setGoodsId(Long.parseLong(storage.getStorageGoodsId().toString()));
            //判断是否限制批次出库
            if(basicCustomer != null && ToolUtil.isNotEmpty(basicCustomer.getGoodsBatchFlag()) && "Yes".equals(basicCustomer.getGoodsBatchFlag())){
                stockParam.setGoodsbatchs(orderDetailedResult.getDetailedBatchnumber());
            }

            stockParam.setStockstate("IN_STOCK");

            //拣货位出库
            if(basicCustomer != null && ToolUtil.isNotEmpty(basicCustomer.getRestockingLimit()) && "Yes".equals(basicCustomer.getRestockingLimit())){
                stockParam.setRestockingLimit("Yes");
            }

            StorageStockResult rStockResult = storageStockService.sumbysqzkk(stockParam);//查询该品 库存

            StorageOutDetailedParam detailedParam  = new StorageOutDetailedParam();
            detailedParam.setStorageGoodsId(Long.parseLong(storage.getStorageGoodsId().toString()));
            detailedParam.setStorageBatchnumber(orderDetailedResult.getDetailedBatchnumber());
            StorageOutDetailedResult  StorageOutDetailedResult = findSumBySpec(detailedParam, outOrder.getOutStorehouseId());//查询待拣货 库存
            BigDecimal onum = new BigDecimal(0);//待拣库存
            if (StorageOutDetailedResult!=null) {
                onum = new BigDecimal(StorageOutDetailedResult.getStorageNumber());
            }
            BigDecimal Num = new BigDecimal(storage.getStorageNumber()).add(onum);//系统库存
            if (rStockResult!=null) {
                BigDecimal num = new BigDecimal(rStockResult.getNum());
                if (Num.compareTo(num)==1) {
                    BigDecimal sub = Num.subtract(num);//系统库存   -   待拣库存 =  实际库存
                    data.setSuccess(false);
                    if(basicCustomer != null && ToolUtil.isNotEmpty(basicCustomer.getRestockingLimit()) && "Yes".equals(basicCustomer.getRestockingLimit())){
                        data.setMessage("Insufficient inventory at picking location! " + outOrder.getCustomerNumber() + "<br>Commodity Code: " + storage.getStorageGoodscode() + "<br>Commodity Name: " + storage.getStorageGoodsname() + "<br>Shortage: " + sub.toString() + " units<br>Batch No.: " + orderDetailedResult.getDetailedBatchnumber());
                    }else{
                        data.setMessage("Insufficient inventory! " + outOrder.getCustomerNumber() + "<br>Commodity Code: " + storage.getStorageGoodsname() + "<br>Commodity Name: " + storage.getStorageGoodsname() + "<br>Shortage: " + sub.toString() + " units<br>Batch No.: " + orderDetailedResult.getDetailedBatchnumber());
                    }
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return data;
                }
            } else {
                data.setSuccess(false);
                if(basicCustomer != null && ToolUtil.isNotEmpty(basicCustomer.getRestockingLimit()) && "Yes".equals(basicCustomer.getRestockingLimit())){
                    data.setMessage("Insufficient inventory at picking location！"+outOrder.getCustomerNumber()+"<br>Commodity Code:"+storage.getStorageGoodscode()+"<br>Commodity Name:"+storage.getStorageGoodsname()+"<br>Shortage:"+Num.toString()+"units<br>Batch No.:"+orderDetailedResult.getDetailedBatchnumber());
                }else{
                    data.setMessage("Insufficient inventory！"+outOrder.getCustomerNumber()+"<br>Commodity Code:"+storage.getStorageGoodscode()+"<br>Commodity Name:"+storage.getStorageGoodsname()+"<br>Shortage:"+Num.toString()+"units<br>Batch No.:"+orderDetailedResult.getDetailedBatchnumber());
                }
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return data;
            }

            solist2.add(storage);
        }
        this.saveBatchList(solist2);
        //更新出库订单
        outOrder.setOutOrderStatus("PENDING_SHIPMENT");
        OutOrderParam param = new OutOrderParam();
        ToolUtil.copyProperties(outOrder,param);
        outOrderService.update(param);
        return ResponseData.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class,timeout=60)
    public ResponseData outSingleHSHN(OutOrder outOrder,ResponseData data) {

        StorageOutParam storageOutParam = new StorageOutParam();
        storageOutParam.setStorageOutNumber(outOrder.getCustomerNumber());
        //出库订单编号
        storageOutParam.setOutOrderNumber(outOrder.getOutOrderNumber());
        //库房id
        storageOutParam.setOutStorehouseId(outOrder.getOutStorehouseId());
        //库房名称
        storageOutParam.setStorehouseName(outOrder.getStorehouseName());
        //收货地址
        storageOutParam.setGetLocation(outOrder.getGetLocation());
        //收货地址id
        storageOutParam.setGetLocationId(outOrder.getGetLocationId());
        //收货单位
        storageOutParam.setGetUnit(outOrder.getGetUnit());
        //收货单位id
        storageOutParam.setGetUnitId(outOrder.getGetUnitId());
        //交付id
        storageOutParam.setDeliverId(outOrder.getDeliverId());
        storageOutParam.setDeptId(outOrder.getDeptId());
        storageOutParam.setOutOrderStatus("INCOMPLETE");

        storageOutParam.setSetMentiontime(outOrder.getSetMentiontime());//发货时间
        //不重复
        String uuidOne = UUID.randomUUID().toString().replaceAll("-","");
        storageOutParam.setRepetition(uuidOne);

        storageOutService.add(storageOutParam);

        //新增拣货单明细
        OutOrderDetailedParam outOrderDetailedParam = new OutOrderDetailedParam();
        outOrderDetailedParam.setDetailedOutNumber(outOrder.getOutOrderNumber());
        List<OutOrderDetailedResult> orderDetailedResultList = outOrderDetailedService.findListBySpec(outOrderDetailedParam);

        BasicCustomer basicCustomer = basicCustomerService.getById(outOrder.getOutCustomerId());
        List<Map<String,String>> bhlist = new ArrayList<Map<String,String>>();//需要补货的商品列表
        List<Map<String,String>> kcbzlist = new ArrayList<Map<String,String>>();//库存不足商品列表
        Map<String,List>  kcbzMap = new HashMap<>();//需要补货及库存不足的商品列表

        List<StorageOutDetailed> solist2 = new ArrayList<StorageOutDetailed>();//要保存的 拣货单明细list
        for (OutOrderDetailedResult orderDetailedResult : orderDetailedResultList){
            StorageOutDetailed storage = prepareViewData(orderDetailedResult);
            storage.setSigns("A");
            storage.setStorageOutNumber(storageOutParam.getStorageOutNumber());//出库单
            storage.setStoragePutbalenum(orderDetailedResult.getDetailedNumber());//大包数
            storage.setStorageNumber(orderDetailedResult.getDetailedNumber());//数量

            StorageStockParam stockParam = new StorageStockParam();
            stockParam.setWarehouseId(outOrder.getOutStorehouseId());
            stockParam.setGoodsId(Long.parseLong(storage.getStorageGoodsId().toString()));
            //判断是否限制批次出库
            if(basicCustomer != null && ToolUtil.isNotEmpty(basicCustomer.getGoodsBatchFlag()) && "Yes".equals(basicCustomer.getGoodsBatchFlag())){
                stockParam.setGoodsbatchs(orderDetailedResult.getDetailedBatchnumber());
            }

            stockParam.setStockstate("IN_STOCK");
            BigDecimal onum = new BigDecimal(0);//待拣库存
            StorageOutDetailedParam detailedParam  = new StorageOutDetailedParam();
            detailedParam.setStorageGoodsId(Long.parseLong(storage.getStorageGoodsId().toString()));
            detailedParam.setStorageBatchnumber(orderDetailedResult.getDetailedBatchnumber());
            StorageOutDetailedResult  StorageOutDetailedResult = findSumBySpec(detailedParam, outOrder.getOutStorehouseId());//查询待拣货 库存
            if (StorageOutDetailedResult!=null) {
                onum = new BigDecimal(StorageOutDetailedResult.getStorageNumber());
            }
            BigDecimal Num = new BigDecimal(storage.getStorageNumber()).add(onum);//系统库存
            //全部库存是否充足
            StorageStockResult rStockResultAll = storageStockService.sumbysqzkk(stockParam);
            if (rStockResultAll!=null) {
                BigDecimal num = new BigDecimal(rStockResultAll.getNum());
                if (Num.compareTo(num)==1) {
                    BigDecimal sub = Num.subtract(num);//系统库存   -   待拣库存 =  实际库存
                    data.setSuccess(false);
                    data.setMessage("Insufficient inventory！"+outOrder.getCustomerNumber()+"<br>Commodity Code:"+storage.getStorageGoodscode()+"<br>Commodity Name:"+storage.getStorageGoodsname()+"<br>Shortage:"+sub.toString()+"units<br>Batch No.:"+orderDetailedResult.getDetailedBatchnumber());
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
//                    return data;
                    //库存不足的商品
                    Map<String,String> kcbz = new HashMap<String,String>();
                    kcbz.put("goodsNumber",storage.getStorageGoodscode());
                    kcbz.put("goodsCount",sub.toString());
                    kcbzlist.add(kcbz);
                    continue;
                }
            } else {
                data.setSuccess(false);
                data.setMessage("Insufficient inventory！"+outOrder.getCustomerNumber()+"<br>Commodity Code:"+storage.getStorageGoodscode()+"<br>Commodity Name:"+storage.getStorageGoodsname()+"<br>Shortage:"+Num.toString()+"units<br>Batch No.:"+orderDetailedResult.getDetailedBatchnumber());
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
//                    return data;
                //库存不足的商品
                Map<String,String> kcbz = new HashMap<String,String>();
                kcbz.put("goodsNumber",storage.getStorageGoodscode());
                kcbz.put("goodsCount",Num.toString());
                kcbzlist.add(kcbz);
                continue;
            }

            //拣货位库存是否充足
            if(basicCustomer != null && ToolUtil.isNotEmpty(basicCustomer.getRestockingLimit()) && "Yes".equals(basicCustomer.getRestockingLimit())){
                stockParam.setRestockingLimit("Yes");
                StorageStockResult rStockResult = storageStockService.sumbysqzkk(stockParam);//查询该品 库存
                if (rStockResult!=null) {
                    BigDecimal num = new BigDecimal(rStockResult.getNum());
                    if (Num.compareTo(num)==1) {
                        BigDecimal sub = Num.subtract(num);//系统库存   -   待拣库存 =  实际库存
                        data.setSuccess(true);
                        data.setCode(7527);
                        Map<String,String> bh = new HashMap<String,String>();
                        bh.put("goodsNumber",storage.getStorageGoodscode());
                        bh.put("goodsCount",Num.toString());
                        bhlist.add(bh);
                        data.setMessage("Insufficient inventory at picking location！"+outOrder.getCustomerNumber()+"<br>Commodity Code:"+storage.getStorageGoodscode()+"<br>Commodity Name:"+storage.getStorageGoodsname()+"<br>Shortage:"+sub.toString()+"units<br>Batch No.:"+orderDetailedResult.getDetailedBatchnumber());
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
//                        return data;
                    }
                } else {
                    data.setSuccess(true);
                    data.setCode(7527);
                    Map<String,String> bh = new HashMap<String,String>();
                    bh.put("goodsNumber",storage.getStorageGoodscode());
                    bh.put("goodsCount",Num.toString());
                    bhlist.add(bh);
                    data.setMessage("Insufficient inventory at picking location！"+outOrder.getCustomerNumber()+"<br>Commodity Code:"+storage.getStorageGoodscode()+"<br>Commodity Name:"+storage.getStorageGoodsname()+"<br>Shortage:"+Num.toString()+"units<br>Batch No.:"+orderDetailedResult.getDetailedBatchnumber());
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
//                    return data;
                }
            }
            solist2.add(storage);
        }
        kcbzMap.put("kcbzlist",kcbzlist);
        kcbzMap.put("bhlist",bhlist);

        if(kcbzlist.size()==0 && bhlist.size()==0){
            this.saveBatchList(solist2);
            //更新出库订单
            outOrder.setOutOrderStatus("PENDING_SHIPMENT");
            OutOrderParam param = new OutOrderParam();
            ToolUtil.copyProperties(outOrder,param);
            outOrderService.update(param);
            data.setSuccess(true);
            data.setCode(200);
            data.setMessage("Outbound successful！"+outOrder.getCustomerNumber());
            data.setData(orderDetailedResultList);
        }else {
            data.setSuccess(true);
            data.setCode(7527);
            data.setData(kcbzMap);
            data.setMessage("Outbound failed！"+outOrder.getCustomerNumber());
        }
        return data;
    }

    /**
     * 拣货单明细
     * @param orderDetailedResult
     * @return
     */
    private StorageOutDetailed prepareViewData2(OutOrderDetailedResult orderDetailedResult){
        StorageOutDetailed storage = new StorageOutDetailed();
        //商品编码
        storage.setStorageGoodscode(orderDetailedResult.getDetailedGoodscode());
        //商品id
        storage.setStorageGoodsId(orderDetailedResult.getDetailedGoodsId());
        //商品名称
        storage.setStorageGoodsname(orderDetailedResult.getDetailedGoodsname());
        //重量
        storage.setStorageWeight(orderDetailedResult.getDetailedWeight());
        //体积
        storage.setStorageVolume(orderDetailedResult.getDetailedVolume());
        //单价
        storage.setStoragePrice(orderDetailedResult.getDetailedPrice());
        //金额
        storage.setStorageMoney(orderDetailedResult.getDetailedMoney());
        //批号
        storage.setStorageBatchnumber(orderDetailedResult.getDetailedBatchnumber());
        //生产日期
        storage.setStorageDate(orderDetailedResult.getDetailedDate());
        //重泡货
        storage.setStorageRebubbles(orderDetailedResult.getDetailedRebubbles());
        //商品包装
        storage.setStoragePacking(orderDetailedResult.getDetailedPacking());
        //商品大类
        storage.setStorageLargeclass(orderDetailedResult.getDetailedLargeclass());
        //商品类别
        storage.setStorageGoodstype(orderDetailedResult.getDetailedGoodstype());
        //保管条件
        storage.setStoragePutcondition(orderDetailedResult.getDetailedPutcondition());
        //出库明细状态
        storage.setStorageOutStatus("PENDING_PICKING");
        return storage;
    }

    /**
     * 拣货单明细
     * @param orderDetailedResult
     * @return
     */
    private StorageOutDetailed prepareViewData(OutOrderDetailedResult orderDetailedResult){
        StorageOutDetailed storage = new StorageOutDetailed();
        //商品编码
        storage.setStorageGoodscode(orderDetailedResult.getDetailedGoodscode());
        //商品id
        storage.setStorageGoodsId(orderDetailedResult.getDetailedGoodsId());
        //商品名称
        storage.setStorageGoodsname(orderDetailedResult.getDetailedGoodsname());
        storage.setGoodsNo(orderDetailedResult.getGoodsNo());
        //重量
        storage.setStorageWeight(orderDetailedResult.getDetailedWeight());
        //体积
        storage.setStorageVolume(orderDetailedResult.getDetailedVolume());
        //单价
        storage.setStoragePrice(orderDetailedResult.getDetailedPrice());
        //金额
        storage.setStorageMoney(orderDetailedResult.getDetailedMoney());
        //批号
        storage.setStorageBatchnumber(orderDetailedResult.getDetailedBatchnumber());
        //生产日期
        storage.setStorageDate(orderDetailedResult.getDetailedDate());
        //重泡货
        storage.setStorageRebubbles(orderDetailedResult.getDetailedRebubbles());
        //商品大类
        storage.setStorageLargeclass(orderDetailedResult.getDetailedLargeclass());
        //商品类别
        storage.setStorageGoodstype(orderDetailedResult.getDetailedGoodstype());
        //保管条件
        storage.setStoragePutcondition(orderDetailedResult.getDetailedPutcondition());
        //单位
        storage.setGoodsUnit(orderDetailedResult.getGoodsUnit());
        //商品包装
        storage.setStoragePacking(orderDetailedResult.getDetailedPacking());
        //规格
        storage.setGoodsSpecs(orderDetailedResult.getGoodsSpecs());
        //出库明细状态
        storage.setStorageOutStatus("PENDING_PICKING");
        return storage;
    }

    @Override
    public LayuiPageInfo findDistinctGoods(StorageOutDetailedParam storageOutDetailedParam) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findDistinctGoods(pageContext, storageOutDetailedParam);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public boolean saveBatchList(List<StorageOutDetailed> list) {
        return this.baseMapper.saveBatchList(list);
    }

    @Override
    public boolean updateBatchList(List<StorageOutDetailed> detailedList) {
        return this.baseMapper.updateBatchList(detailedList);
    }

    @Override
    public List<StorageOutDetailedResult> findBystorageOutNumber(String[] storageOutNumbers) {
        return  this.baseMapper.findBystorageOutNumber(storageOutNumbers);
    }

    @Override
    public List<StorageOutDetailedResult> findBatchstock(String[] storageOutNumbers, Long goodsid) {
        return  this.baseMapper.findBatchstock(storageOutNumbers,goodsid);
    }

    @Override
    public LayuiPageInfo findByStorageOutNumbersPage(String storageOutNumbers) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findByStorageOutNumbersPage(pageContext,storageOutNumbers);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     *出库审核拣货单按库位查询商品
     * @return
     */
    @Override
    public LayuiPageInfo findByapporderReview(String[] storageOutNumbers) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findByapporderReview(pageContext,storageOutNumbers);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<StorageOutDetailedResult> findByTrayList(String[] storageOutNumbers, Long goodsid, String storagePostionNumber) {
        return  this.baseMapper.findByTrayList( storageOutNumbers,goodsid,storagePostionNumber);
    }

    @Override
    public List<StorageOutDetailedResult> findByTrayListReview(String[] storageOutNumbers, Long goodsid) {
        return  this.baseMapper.findByTrayListReview( storageOutNumbers,goodsid);
    }

    @Override
    public List<StorageOutDetailedResult> findName(Long deptId , Long deliverId) {
        return  this.baseMapper.findName(deptId , deliverId);
    }

    @Override
    public List<StorageOutDetailedResult> findGoodsByName(Long deptId , Long deliverId,StorageOutDetailedParam storageoutdetailedparam) {
        return  this.baseMapper.findGoodsByName(deptId , deliverId,storageoutdetailedparam);
    }

    @Override
    public List<StorageOutDetailedResult> findBysON(String  storageOutNumber) {
        return  this.baseMapper.findBysON(storageOutNumber);
    }

    @Override
    public LayuiPageInfo findByMark(StorageOutParam storageOutParam, String condition) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findByMark(pageContext,storageOutParam,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<StorageOutDetailedResult> getPrintstorageOutDetailed(StorageOutDetailedParam storageOutDetailedParam) {
        return this.baseMapper.getPrintstorageOutDetailed(storageOutDetailedParam);
    }

    /**
     * 生成 推荐库位
     */
    @Override
    @Transactional(rollbackFor = Exception.class,timeout=200)
    public ResponseData
    recommend(List<StorageOutResult> list) {
        ResponseData data = new ResponseData();
        List<StorageOutDetailedResult> listss = new ArrayList<StorageOutDetailedResult>();//要推荐库位的  拣货明细
        for (StorageOutResult storageOutResult : list) {
            StorageOutDetailedParam param22 = new StorageOutDetailedParam();
            param22.setStorageOutNumber(storageOutResult.getStorageOutNumber());
            param22.setStorageOutStatus("PENDING_PICKING");
            List<StorageOutDetailedResult> dlist = this.findListBySpec(param22);
            List<Long> sids =new ArrayList<>();
            for (StorageOutDetailedResult rr : dlist ) {
                sids.add(Long.parseLong(rr.getId().toString()));
                listss.add(rr);
            }
            this.deletebyids(sids);//删除  拣货明细
        }

        List<StorageOutDetailed> solist = new ArrayList<StorageOutDetailed>();//完成库位推荐的   拣货明细
        for (StorageOutDetailedResult rrr : listss) {
            StorageOutDetailedParam detailedParam = new StorageOutDetailedParam();

            ToolUtil.copyProperties(rrr,detailedParam);
            detailedParam.setStorageOutStatus("PENDING_PICKING");
            String str = detailedParam.getStorageOutNumber();//拣货单
            String[]  strs=str.split(",");
            StorageOutResult outResult = new StorageOutResult();
            for(int i=0,len=strs.length;i<len;i++) {
                StorageOutParam outParam = new StorageOutParam();
                outParam.setStorageOutNumber(strs[i].toString());
                outResult = storageOutService.findBySpec(outParam);
            }

            BasicGoods basicGoods = basicGoodsService.getById(detailedParam.getStorageGoodsId());
            BasicCustomer basicCustomer = basicCustomerService.getById(basicGoods.getGoodsCustomerId());
            //出库数量
            BigDecimal outNumber = new BigDecimal(detailedParam.getStorageNumber());
            //单价
            BigDecimal price = new BigDecimal(basicGoods.getGoodsCharging());

            //查询 库存
            //规则：1.如果指定批次，优先出库   2.散托优先出库    3.批次较早，优先出库  4.批次较早的比散托的优先级高
            StorageStockParam sp = new StorageStockParam();//查询 库存单明细 where 商品,仓库id。
            sp.setWarehouseId(outResult.getOutStorehouseId());//仓库id
            sp.setGoodsId(Long.parseLong(basicGoods.getId().toString()));//商品id

            OutOrderDetailedParam outOrderDetailedParam = new OutOrderDetailedParam();
            outOrderDetailedParam.setDetailedOutNumber(outResult.getOutOrderNumber());
            outOrderDetailedParam.setDetailedGoodsId(Long.parseLong(detailedParam.getStorageGoodsId().toString()));
            outOrderDetailedParam.setDetailedBatchnumber(detailedParam.getStorageBatchnumber());


            OutOrderDetailedResult outOrderDetailedResult =  outOrderDetailedService.findBySpec(outOrderDetailedParam);
            if(outOrderDetailedResult == null) {
                outOrderDetailedParam.setDetailedBatchnumber(null);
                outOrderDetailedResult =  outOrderDetailedService.findBySpec(outOrderDetailedParam);
            }
            if(outOrderDetailedResult != null) {
                if(ToolUtil.isNotEmpty(outOrderDetailedResult.getDetailedBatchnumber())) {
                    if(basicCustomer != null && ToolUtil.isNotEmpty(basicCustomer.getGoodsBatchFlag()) && "Yes".equals(basicCustomer.getGoodsBatchFlag())){
                        sp.setGoodsbatchs(detailedParam.getStorageBatchnumber());
                    }
                }
            }

            // 限定拣货库位出库
            if(basicCustomer != null && ToolUtil.isNotEmpty(basicCustomer.getRestockingLimit()) && "Yes".equals(basicCustomer.getRestockingLimit())){
                sp.setRestockingLimit("Yes");
            }

            List<StorageStockResult> srl = storageStockService.finByOnlyOne(sp);

            int srls = 0;
            BigDecimal result = new BigDecimal("0");
            Long stockId = 0L;
            while (outNumber.compareTo(result) == 1){
                if (srls==srl.size()||srl==null) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
                    data.setSuccess(false);
                    data.setMessage(detailedParam.getStorageGoodscode()+",Insufficient inventory!");
                    return data;
                }
                StorageStockResult stock = srl.get(srls);

                StorageOutDetailed storage = new StorageOutDetailed();
                ToolUtil.copyProperties(detailedParam,storage);
                storage.setSigns("B");
                storage.setSigns2(null);
                storage.setCreateTime(new Date());
                BigDecimal putNum = new BigDecimal(stock.getNum());//托盘商品数量

                if (putNum.compareTo(result) == 0) {
                    srls++;
                    continue;
                }

                //去除被占用的库存
                if(ToolUtil.isNotEmpty(basicCustomer.getOccupy()) && basicCustomer.getOccupy().equals("Yes")) {
                    StorageOutDetailedParam detailedParam2 = new StorageOutDetailedParam();
                    detailedParam2.setStockId(Long.parseLong(stock.getId().toString()));
                    detailedParam2.setStorageOutStatus("PENDING_PICKING");
                    StorageOutDetailedResult result2 = this.sumByStockId(detailedParam2);
                    if(result2 != null) {
                        BigDecimal rr2 = new BigDecimal(result2.getStorageNumber());
                        if(rr2.compareTo(result) == 1) {
                            if(putNum.compareTo(rr2) == 1) {
                                putNum = putNum.subtract(rr2);
                            } else {
                                srls++;
                                continue;
                            }
                        }
                    }
                } else if (ToolUtil.isNotEmpty(outResult.getMark())){//如果合单  去除被合单占用的库存
                    StorageOutDetailedParam detailedParam2 = new StorageOutDetailedParam();
                    detailedParam2.setStockId(Long.parseLong(stock.getId().toString()));
                    detailedParam2.setStorageOutStatus("PENDING_PICKING");
                    StorageOutDetailedResult result2 = this.sumByStockId2(detailedParam2,outResult.getMark());
                    if(result2 != null) {
                        BigDecimal rr2 = new BigDecimal(result2.getStorageNumber());
                        if(rr2.compareTo(result) == 1) {
                            if(putNum.compareTo(rr2) == 1) {
                                putNum = putNum.subtract(rr2);
                            } else {
                                srls++;
                                continue;
                            }
                        }
                    }
                } else {//去除被同一单子占用的库存
                    StorageOutDetailedParam detailedParam2 = new StorageOutDetailedParam();
                    detailedParam2.setStockId(Long.parseLong(stock.getId().toString()));
                    detailedParam2.setStorageOutStatus("PENDING_PICKING");
                    detailedParam2.setStorageOutNumber(detailedParam.getStorageOutNumber());
                    StorageOutDetailedResult result2 = this.sumByStockId(detailedParam2);
                    if(result2 != null) {
                        BigDecimal rr2 = new BigDecimal(result2.getStorageNumber());
                        if(rr2.compareTo(result) == 1) {
                            if(putNum.compareTo(rr2) == 1) {
                                putNum = putNum.subtract(rr2);
                            } else {
                                srls++;
                                continue;
                            }
                        }
                    }
                }

                //这一托可以出完
                if(outNumber.subtract(putNum).compareTo(result) == -1||outNumber.subtract(putNum).compareTo(result)== 0){
                    //重量
                    BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
                    //体积
                    BigDecimal v = new BigDecimal(basicGoods.getGoodsVolume());
                    if (putNum.subtract(outNumber).compareTo(result) == 1){
                        stockId = Long.parseLong(stock.getId().toString());//库存id

                        //完善 拣货单明细
                        storage.setTrayNumber(stock.getTrayNumber());//托盘编号
                        storage.setStorageBatchnumber(stock.getGoodsbatchs());//批次
                        storage.setStorageDate(stock.getProducetime());//生产日期
                        storage.setRecommendBatchs(stock.getPutTime());//推荐批次
                        storage.setStoragePostionNumber(stock.getPositionNumber());//库位编码
                        storage.setStorageNumber(String.valueOf(outNumber));//出库数量为拣货单数量
                        storage.setStoragePutbalenum(String.valueOf(outNumber));//出库数量为拣货单大包数
                        storage.setStorageWeight(String.valueOf(outNumber.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总重量
                        storage.setStorageVolume(String.valueOf(outNumber.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总体积
                        storage.setStorageMoney(String.valueOf(price.multiply(outNumber).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                        storage.setStockId(stockId);//库存id
                        solist.add(storage);

                        BigDecimal outNumber1	=  outNumber.subtract(putNum).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                        outNumber = outNumber1;//减 出库数量
                        srls++;
                    } else {
                        stockId = Long.parseLong(stock.getId().toString());//库存id
                        //完善 拣货单明细
                        storage.setTrayNumber(stock.getTrayNumber());//托盘编号
                        storage.setStorageBatchnumber(stock.getGoodsbatchs());//批次
                        storage.setStorageDate(stock.getProducetime());//生产日期
                        storage.setRecommendBatchs(stock.getPutTime());//推荐批次
                        storage.setStoragePostionNumber(stock.getPositionNumber());//库位编码
                        storage.setStorageNumber(String.valueOf(outNumber));//出库数量为拣货单数量
                        storage.setStoragePutbalenum(String.valueOf(outNumber));//出库数量为拣货单大包数
                        storage.setStorageWeight(String.valueOf(outNumber.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总重量
                        storage.setStorageVolume(String.valueOf(outNumber.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总体积
                        storage.setStorageMoney(String.valueOf(price.multiply(outNumber).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                        storage.setStockId(stockId);//库存id
                        solist.add(storage);

                        BigDecimal outNumber1	=  outNumber.subtract(putNum).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                        outNumber = outNumber1;//减 出库数量
                        srls++;
                    }
                }else{
                    //重量
                    BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
                    //体积
                    BigDecimal v = new BigDecimal(basicGoods.getGoodsVolume());
                    //这托出不完
                    stockId = Long.parseLong(stock.getId().toString());//库存id

                    BigDecimal outNumber1	=  outNumber.subtract(putNum).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                    outNumber = outNumber1;//减 出库数量

                    //完善 拣货单明细
                    storage.setTrayNumber(stock.getTrayNumber());
                    storage.setStorageBatchnumber(stock.getGoodsbatchs());//批次
                    storage.setStorageDate(stock.getProducetime());//生产日期
                    storage.setRecommendBatchs(stock.getPutTime());//推荐批次
                    storage.setStoragePostionNumber(stock.getPositionNumber());
                    storage.setStoragePutbalenum(putNum.toString());
                    storage.setStorageNumber(putNum.toString());
                    storage.setStorageWeight(String.valueOf(putNum.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                    storage.setStorageVolume(String.valueOf(putNum.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                    storage.setStorageMoney(String.valueOf(price.multiply(putNum).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                    storage.setStockId(stockId);//库存id
                    solist.add(storage);
                    srls++;
                }
                stock = new StorageStockResult();
                storage = new StorageOutDetailed();
            }
            if (solist.size() > 0) {
                this.saveBatchList(solist);
            }
            solist = new ArrayList<StorageOutDetailed>();
        }

        //合并同一托拣货明细
        for (StorageOutResult storageOutResult : list) {
            String[] strs2 = storageOutResult.getStorageOutNumber().split(",");
            StorageOutDetailedParam storageOutDetailedParamp = new StorageOutDetailedParam();
            List<StorageOutDetailedResult>  list2 =  this.findgoodsbystrs(storageOutDetailedParamp,strs2);//合计明细  按库存id和订单号去重
            for (StorageOutDetailedResult r2 : list2) {
                if(r2.getStockId()==null) {
                    continue;
                }
                StorageOutDetailedParam param2 = new StorageOutDetailedParam();
                param2.setStockId(r2.getStockId());
                param2.setStorageOutNumber(r2.getStorageOutNumber());
                param2.setStorageGoodsId(r2.getStorageGoodsId());
                List<StorageOutDetailedResult>  list3 =  this.findListStockIdBySpec(param2);//查询是否存在多条
                if(list3.size()>=2) {
                    for (int i = 0; i < list3.size(); i++) {
                        StorageOutDetailedParam storageOutDetailedParam3 = new StorageOutDetailedParam();
                        storageOutDetailedParam3.setId(list3.get(i).getId());
                        this.delete(storageOutDetailedParam3);
                    }
                    StorageOutDetailedParam storageOutDetailedParam2 = new StorageOutDetailedParam();
                    StorageOutDetailedResult storageOutDetailedResult = list3.get(0);
                    storageOutDetailedResult.setId(null);
                    storageOutDetailedResult.setStoragePutbalenum(r2.getStoragePutbalenum());
                    storageOutDetailedResult.setStorageNumber(r2.getStorageNumber());
                    storageOutDetailedResult.setStorageWeight(r2.getStorageWeight());
                    storageOutDetailedResult.setStorageVolume(r2.getStorageVolume());
                    storageOutDetailedResult.setStorageMoney(r2.getStorageMoney());
                    ToolUtil.copyProperties(storageOutDetailedResult, storageOutDetailedParam2);
                    storageOutDetailedParam2.setSigns("C");
                    this.add(storageOutDetailedParam2);
                }
            }
        }
        data.setSuccess(true);
        return data;
    }

    public StorageOutDetailedResult sumByStockId2(StorageOutDetailedParam detailedParam2, String mark) {
        return this.baseMapper.sumByStockId2(detailedParam2,mark);
    }

    /**
     * 复原
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData restore(StorageOutParam storageOutParam, ResponseData data) {
        StorageOutResult result22 = storageOutService.findBySpec(storageOutParam);

        StorageOutDetailedParam param22 = new StorageOutDetailedParam();
        param22.setStorageOutNumber(storageOutParam.getStorageOutNumber());
        List<StorageOutDetailedResult> dlist = this.findListBySpec(param22);
        List<Long > sids =new ArrayList<>();
        for (StorageOutDetailedResult r : dlist) {
            sids.add(Long.parseLong(r.getId().toString()));
        }
        boolean a = this.removeByIds(sids);
        if(!a) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
            data.setSuccess(false);
            data.setMessage("Data exception");
            return  data;
        }
        //新增拣货单明细
        OutOrderDetailedParam outOrderDetailedParam = new OutOrderDetailedParam();
        outOrderDetailedParam.setDetailedOutNumber(result22.getOutOrderNumber());
        List<OutOrderDetailedResult> orderDetailedResultList = outOrderDetailedService.findListBySpec(outOrderDetailedParam);

        List<StorageOutDetailed> solist2 = new ArrayList<StorageOutDetailed>();//要保存的 拣货单明细list
        for (OutOrderDetailedResult orderDetailedResult : orderDetailedResultList ) {
            StorageOutDetailed storage = prepareViewData(orderDetailedResult);
            storage.setStorageOutNumber(storageOutParam.getStorageOutNumber());//出库单
            storage.setStoragePutbalenum(orderDetailedResult.getDetailedNumber());//大包数
            storage.setStorageNumber(orderDetailedResult.getDetailedNumber());//数量
            solist2.add(storage);
        }
        if (solist2.size() > 0) {
            this.saveBatchList(solist2);
        }
        if (data.getSuccess()==null) {
            data.setSuccess(true);
        }
        return data;
    }

    @Override
    public int deletebyids(List<Long> list) {
        return this.baseMapper.deletebyids(list);

    }

    @Override
    public void unlockTheItem() {
        StorageOutDetailedParam  storageOutDetailedParam =new StorageOutDetailedParam();
        storageOutDetailedParam.setCurrentOperator(String.valueOf(userService.getcurrentUser().getAccount()));
        List<StorageOutDetailedResult> storageOutDetailedResult =baseMapper.findListBySpec(storageOutDetailedParam);
        for (int i = 0; i < storageOutDetailedResult.size(); i++) {
            StorageOutDetailedResult aa = storageOutDetailedResult.get(i);
            aa.setCurrentOperator("");
            aa.setPickingStatus("");
            StorageOutDetailedParam storageOutDetailedParams = new StorageOutDetailedParam();
            ToolUtil.copyProperties(aa, storageOutDetailedParams);
            this.update(storageOutDetailedParams);
        }
    }

    @Override
    public void unlockTheItemNew(User user) {
        StorageOutDetailedParam  storageOutDetailedParam =new StorageOutDetailedParam();
        storageOutDetailedParam.setCurrentOperator(String.valueOf(user.getAccount()));
        List<StorageOutDetailedResult> storageOutDetailedResult =baseMapper.findListBySpec(storageOutDetailedParam);
        for (int i = 0; i < storageOutDetailedResult.size(); i++) {
            StorageOutDetailedResult aa = storageOutDetailedResult.get(i);
            StorageOutDetailedParam storageOutDetailedParams = new StorageOutDetailedParam();
            storageOutDetailedParams.setId(aa.getId());
            storageOutDetailedParams.setCurrentOperator("");
            storageOutDetailedParams.setPickingStatus("");
            this.update(storageOutDetailedParams);
        }
    }

    @Override
    public List<StorageOutDetailedResult> trayInformation(StorageOutDetailedParam storageOutDetailedParam,String[] strs) {
        return this.baseMapper.trayInformation( storageOutDetailedParam,strs);
    }

    /**
     * 校验库存完整
     */
    public List<StorageOutDetailedResult> completeNew(StorageOutDetailedResult detailedParam,User user) {
        StorageStock storageStock = storageStockService.getById(detailedParam.getStockId());//托盘
        StorageOutDetailed storageOutDetailed = this.getById(detailedParam.getId());
        if(storageOutDetailed != null) {
            if(ToolUtil.isNotEmpty(storageOutDetailed.getRealNum())) {
                return null;
            }
        }

        String str = detailedParam.getStorageOutNumber();//拣货单
        String[]  strs=str.split(",");
        StorageOutResult outResult = new StorageOutResult();
        for(int i=0,len=strs.length;i<len;i++) {
            StorageOutParam outParam = new StorageOutParam();
            outParam.setStorageOutNumber(strs[i].toString());
            outResult = storageOutService.findBySpec(outParam);
            break;
        }

        StorageOutDetailedParam param = new StorageOutDetailedParam();
        param.setStorageOutNumber(detailedParam.getStorageOutNumber());
        param.setStorageGoodscode(detailedParam.getStorageGoodscode());
        param.setStorageOutStatus("PENDING_PICKING");

        if(ToolUtil.isNotEmpty(outResult.getMark())) {
            StorageOutDetailedParam detailedParam2 = new StorageOutDetailedParam();
            detailedParam2.setStockId(Long.parseLong(storageStock.getId().toString()));
            detailedParam2.setStorageOutStatus("PENDING_PICKING");
            StorageOutDetailedResult storageOutDetailedResult = this.sumByStockId2(detailedParam2,outResult.getMark());
            if (storageStock!=null) {
                if(storageStock.getStockstate().equals("IN_STOCK")) {
                    BigDecimal num1 = new BigDecimal(storageStock.getNum());
                    BigDecimal num2 = new BigDecimal(storageOutDetailedResult.getStorageNumber());
                    if (num1.compareTo(num2)==1||num1.compareTo(num2)==0) {
                        List<StorageOutDetailedResult> list = new ArrayList<StorageOutDetailedResult>();
                        list.add(detailedParam);
                        return list;
                    }
                }
            }
        } else {
            if (storageStock!=null) {
                if(storageStock.getStockstate().equals("IN_STOCK")) {
                    BigDecimal num1 = new BigDecimal(storageStock.getNum());
                    BigDecimal num2 = new BigDecimal(detailedParam.getStorageNumber());
                    if (num1.compareTo(num2)==1||num1.compareTo(num2)==0) {
                        List<StorageOutDetailedResult> list = new ArrayList<StorageOutDetailedResult>();
                        list.add(detailedParam);
                        return list;
                    }
                }
            }
        }

        List<StorageOutDetailed> solist = new ArrayList<StorageOutDetailed>();//要保存的 拣货单明细list

        BasicGoods basicGoods = basicGoodsService.getById(detailedParam.getStorageGoodsId());
        BasicCustomer basicCustomer = basicCustomerService.getById(basicGoods.getGoodsCustomerId());
        BigDecimal u = new BigDecimal(basicGoods.getGoodsCharging());
        //出库数量
        BigDecimal outNumber = new BigDecimal(detailedParam.getStorageNumber());

        //查询 库存
        //规则：1.如果指定批次，优先出库   2.散托优先出库    3.批次较早，优先出库  4.批次较早的比散托的优先级高
        StorageStockParam sp = new StorageStockParam();//查询 库存单明细 where 商品,仓库id。

        OutOrderDetailedParam outOrderDetailedParam = new OutOrderDetailedParam();
        outOrderDetailedParam.setDetailedOutNumber(outResult.getOutOrderNumber());
        outOrderDetailedParam.setDetailedGoodsId(Long.parseLong(detailedParam.getStorageGoodsId().toString()));
        OutOrderDetailedResult outOrderDetailedResult =  outOrderDetailedService.findBySpec(outOrderDetailedParam);
        if(outOrderDetailedResult != null) {
            if(ToolUtil.isNotEmpty(outOrderDetailedResult.getDetailedBatchnumber())  ) {
                sp.setGoodsbatchs(detailedParam.getStorageBatchnumber());
            }
        }
        sp.setWarehouseId(outResult.getOutStorehouseId());//仓库id
        sp.setPositionNumber(detailedParam.getStoragePostionNumber());
        sp.setGoodsId(Long.parseLong(basicGoods.getId().toString()));//商品id
        List<StorageStockResult> srl = storageStockService.finByOnlyOne(sp);

        int srls = 0;
        BigDecimal result = new BigDecimal(0);
        Long stockId = 0L;
        while (outNumber.compareTo(result) == 1){
            if (srls==srl.size()||srl==null) {
                //System.out.println("库存不足");
                return this.findListBySpec(param);
            }
            StorageStockResult stock = srl.get(srls);

            StorageOutDetailed storage = new StorageOutDetailed();
            ToolUtil.copyProperties(detailedParam,storage);
            storage.setSigns("D");
            storage.setSigns2(null);
            storage.setCreateTime(new Date());
            BigDecimal putNum = new BigDecimal(stock.getNum());//托盘商品数量

            if (putNum.compareTo(result)== 0 ) {
                srls++;
                continue;
            }

            //去除被占用的库存
            if(ToolUtil.isNotEmpty(basicCustomer.getOccupy()) && basicCustomer.getOccupy().equals("Yes")) {
                StorageOutDetailedParam detailedParam2 = new StorageOutDetailedParam();
                detailedParam2.setId(detailedParam.getId());
                detailedParam2.setStockId(Long.parseLong(stock.getId().toString()));
                detailedParam2.setStorageOutStatus("PENDING_PICKING");
                StorageOutDetailedResult result2 = this.sumByStockId(detailedParam2);
                if(result2 != null) {
                    BigDecimal rr2 = new BigDecimal(result2.getStorageNumber());
                    if(rr2.compareTo(result) == 1) {
                        if(putNum.compareTo(rr2) == 1) {
                            putNum = putNum.subtract(rr2);
                        } else {
                            srls++;
                            continue;
                        }
                    }
                }
            } else if (ToolUtil.isNotEmpty(outResult.getMark())){//如果合单  去除被合单占用的库存
                StorageOutDetailedParam detailedParam2 = new StorageOutDetailedParam();
                detailedParam2.setId(detailedParam.getId());
                detailedParam2.setStockId(Long.parseLong(stock.getId().toString()));
                detailedParam2.setStorageOutStatus("PENDING_PICKING");
                StorageOutDetailedResult result2 = this.sumByStockId2(detailedParam2,outResult.getMark());
                if(result2 != null) {
                    BigDecimal rr2 = new BigDecimal(result2.getStorageNumber());
                    if(rr2.compareTo(result) == 1) {
                        if(putNum.compareTo(rr2) == 1) {
                            putNum = putNum.subtract(rr2);
                        } else {
                            srls++;
                            continue;
                        }
                    }
                }
            } else {//去除被同一单子占用的库存
                StorageOutDetailedParam detailedParam2 = new StorageOutDetailedParam();
                detailedParam2.setId(detailedParam.getId());
                detailedParam2.setStockId(Long.parseLong(stock.getId().toString()));
                detailedParam2.setStorageOutStatus("PENDING_PICKING");
                detailedParam2.setStorageOutNumber(detailedParam.getStorageOutNumber());
                StorageOutDetailedResult result2 = this.sumByStockId(detailedParam2);
                if(result2 != null) {
                    BigDecimal rr2 = new BigDecimal(result2.getStorageNumber());
                    if(rr2.compareTo(result) == 1) {
                        if(putNum.compareTo(rr2) == 1) {
                            putNum = putNum.subtract(rr2);
                        } else {
                            srls++;
                            continue;
                        }
                    }
                }
            }

            //这一托可以出完
            if(outNumber.subtract(putNum).compareTo(result) == -1||outNumber.subtract(putNum).compareTo(result)== 0){
                //重量
                BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
                //体积
                BigDecimal v = new BigDecimal(basicGoods.getGoodsVolume());
                if (putNum.subtract(outNumber).compareTo(result) == 1){
                    stockId = Long.parseLong(stock.getId().toString());//库存id

                    //完善 拣货单明细
                    storage.setTrayNumber(stock.getTrayNumber());//托盘编号
                    storage.setStorageBatchnumber(stock.getGoodsbatchs());//批次
                    storage.setStorageDate(stock.getProducetime());//生产日期
                    storage.setRecommendBatchs(stock.getPutTime());//推荐批次
                    storage.setStoragePostionNumber(stock.getPositionNumber());//库位编码
                    storage.setStorageNumber(String.valueOf(outNumber));//出库数量为拣货单数量
                    storage.setStoragePutbalenum(String.valueOf(outNumber));//出库数量为拣货单大包数
                    storage.setStorageWeight(String.valueOf(outNumber.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总重量
                    storage.setStorageVolume(String.valueOf(outNumber.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总体积
                    storage.setStorageMoney(String.valueOf(u.multiply(outNumber).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                    storage.setStockId(stockId);//库存id
                    storage.setPickingStatus(DateUtil.getDateS());
                    storage.setCurrentOperator(user.getAccount());
                    solist.add(storage);

                    BigDecimal outNumber1	=  outNumber.subtract(putNum);
                    outNumber = outNumber1;//减 出库数量
                    srls++;
                } else {
                    stockId = Long.parseLong(stock.getId().toString());//库存id
                    //完善 拣货单明细
                    storage.setTrayNumber(stock.getTrayNumber());//托盘编号
                    storage.setStorageBatchnumber(stock.getGoodsbatchs());//批次
                    storage.setStorageDate(stock.getProducetime());//生产日期
                    storage.setRecommendBatchs(stock.getPutTime());//推荐批次
                    storage.setStoragePostionNumber(stock.getPositionNumber());//库位编码
                    storage.setStorageNumber(String.valueOf(outNumber));//出库数量为拣货单数量
                    storage.setStoragePutbalenum(String.valueOf(outNumber));//出库数量为拣货单大包数
                    storage.setStorageWeight(String.valueOf(outNumber.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总重量
                    storage.setStorageVolume(String.valueOf(outNumber.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总体积
                    storage.setStorageMoney(String.valueOf(u.multiply(outNumber).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                    storage.setStockId(stockId);//库存id
                    storage.setPickingStatus(DateUtil.getDateS());
                    storage.setCurrentOperator(user.getAccount());
                    solist.add(storage);

                    BigDecimal outNumber1	=  outNumber.subtract(putNum);
                    outNumber = outNumber1;//减 出库数量
                    srls++;
                }
            }else{
                //重量
                BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
                //体积
                BigDecimal v = new BigDecimal(basicGoods.getGoodsVolume());
                //这托出不完
                stockId = Long.parseLong(stock.getId().toString());//库存id

                BigDecimal outNumber1	=  outNumber.subtract(putNum);
                outNumber = outNumber1;//减 出库数量

                //完善 拣货单明细
                storage.setTrayNumber(stock.getTrayNumber());
                storage.setStorageBatchnumber(stock.getGoodsbatchs());//批次
                storage.setStorageDate(stock.getProducetime());//生产日期
                storage.setRecommendBatchs(stock.getPutTime());//推荐批次
                storage.setStoragePostionNumber(stock.getPositionNumber());
                storage.setStoragePutbalenum(putNum.toString());
                storage.setStorageNumber(putNum.toString());
                storage.setStorageWeight(String.valueOf(putNum.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总重量
                storage.setStorageVolume(String.valueOf(putNum.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总体积
                storage.setStorageMoney(String.valueOf(u.multiply(putNum).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                storage.setStockId(stockId);//库存id
                storage.setPickingStatus(DateUtil.getDateS());
                storage.setCurrentOperator(user.getAccount());
                solist.add(storage);
                srls++;
            }
            stock = new StorageStockResult();
            storage = new StorageOutDetailed();
        }
        if ( solist.size() > 0) {
            this.saveBatchList(solist);
        }
        StorageOutDetailedParam detailedParam2 = new StorageOutDetailedParam();
        detailedParam2.setId(detailedParam.getId());
        this.delete(detailedParam2);


        //合并同一托拣货明细
        String[] strs2 = detailedParam.getStorageOutNumber().split(",");
        StorageOutDetailedParam storageOutDetailedParamp = new StorageOutDetailedParam();
        storageOutDetailedParamp.setStorageGoodsId(detailedParam.getStorageGoodsId());
        List<StorageOutDetailedResult>  list2 =  this.findgoodsbystrs(storageOutDetailedParamp,strs2);//合计明细  按库存id和订单号去重
        for (StorageOutDetailedResult r2 : list2) {
            if(r2.getStockId()==null) {
                continue;
            }
            StorageOutDetailedParam param2 = new StorageOutDetailedParam();
            param2.setStockId(r2.getStockId());
            param2.setStorageOutNumber(r2.getStorageOutNumber());
            param2.setStorageGoodsId(r2.getStorageGoodsId());
            List<StorageOutDetailedResult>  list3 =  this.findListStockIdBySpec(param2);//查询是否存在多条
            if(list3.size()>=2) {
                for (int i = 0; i < list3.size(); i++) {
                    StorageOutDetailedParam storageOutDetailedParam3 = new StorageOutDetailedParam();
                    storageOutDetailedParam3.setId(list3.get(i).getId());
                    this.delete(storageOutDetailedParam3);
                }
                StorageOutDetailedParam storageOutDetailedParam2 = new StorageOutDetailedParam();
                StorageOutDetailedResult storageOutDetailedResult = list3.get(0);
                storageOutDetailedResult.setId(null);
                storageOutDetailedResult.setStoragePutbalenum(r2.getStoragePutbalenum());
                storageOutDetailedResult.setStorageNumber(r2.getStorageNumber());
                storageOutDetailedResult.setStorageWeight(r2.getStorageWeight());
                storageOutDetailedResult.setStorageVolume(r2.getStorageVolume());
                storageOutDetailedResult.setStorageMoney(r2.getStorageMoney());
                ToolUtil.copyProperties(storageOutDetailedResult, storageOutDetailedParam2);
                storageOutDetailedParam2.setSigns("E");
                this.add(storageOutDetailedParam2);
            }
        }

        return this.findListBySpec(param);
    }

    /**
     * 校验库存完整
     */
    public List<StorageOutDetailedResult> complete(StorageOutDetailedResult detailedParam) {
        StorageStock storageStock = storageStockService.getById(detailedParam.getStockId());//托盘

        StorageOutDetailedParam param = new StorageOutDetailedParam();
        param.setStorageOutNumber(detailedParam.getStorageOutNumber());
        param.setStorageGoodscode(detailedParam.getStorageGoodscode());
        param.setStorageOutStatus("PENDING_PICKING");

        if (storageStock!=null) {
            BigDecimal num1 = new BigDecimal(storageStock.getNum());
            BigDecimal num2 = new BigDecimal(detailedParam.getStorageNumber());
            if (num1.compareTo(num2)==1||num1.compareTo(num2)==0) {
                List<StorageOutDetailedResult> list = new ArrayList<StorageOutDetailedResult>();
                list.add(detailedParam);
                return list;
            }
        }

        List<StorageOutDetailed> solist = new ArrayList<StorageOutDetailed>();//要保存的 拣货单明细list
        String str = detailedParam.getStorageOutNumber();//拣货单
        String[]  strs=str.split(",");
        StorageOutResult outResult = new StorageOutResult();
        for(int i=0,len=strs.length;i<len;i++) {
            StorageOutParam outParam = new StorageOutParam();
            outParam.setStorageOutNumber(strs[i].toString());
            outResult = storageOutService.findBySpec(outParam);
        }

        BasicGoods basicGoods = basicGoodsService.getById(detailedParam.getStorageGoodsId());
        //出库数量
        BigDecimal outNumber = new BigDecimal(detailedParam.getStorageNumber());

        //查询 库存
        //规则：1.如果指定批次，优先出库   2.散托优先出库    3.批次较早，优先出库  4.批次较早的比散托的优先级高
        StorageStockParam sp = new StorageStockParam();//查询 库存单明细 where 商品,仓库id。
        sp.setWarehouseId(outResult.getOutStorehouseId());//仓库id
        sp.setPositionNumber(detailedParam.getStoragePostionNumber());
        sp.setGoodsId(Long.parseLong(basicGoods.getId().toString()));//商品id
        List<StorageStockResult> srl = storageStockService.finByOnlyOne(sp);

        int srls = 0;
        BigDecimal result = new BigDecimal(0);
        Long stockId = 0L;
        while (outNumber.compareTo(result) == 1){
            if (srls==srl.size()||srl==null) {
                //System.out.println("库存不足");
                return this.findListBySpec(param);
            }
            StorageStockResult stock = srl.get(srls);
            BigDecimal u = new BigDecimal(1);
            //单价
            if (ToolUtil.isNotEmpty(stock.getUnitprice())) {
                u = new BigDecimal(stock.getUnitprice());
            }

            StorageOutDetailed storage = new StorageOutDetailed();
            ToolUtil.copyProperties(detailedParam,storage);
            BigDecimal putNum = new BigDecimal(stock.getNum());//托盘商品数量

            if (putNum.compareTo(result)== 0 ) {
                srls++;
                continue;
            }

            //这一托可以出完
            if(outNumber.subtract(putNum).compareTo(result) == -1||outNumber.subtract(putNum).compareTo(result)== 0){
                //重量
                BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
                //体积
                BigDecimal v = new BigDecimal(basicGoods.getGoodsVolume());
                if (putNum.subtract(outNumber).compareTo(result) == 1){
                    stockId = Long.parseLong(stock.getId().toString());//库存id

                    //完善 拣货单明细
                    storage.setTrayNumber(stock.getTrayNumber());//托盘编号
                    storage.setStorageBatchnumber(stock.getGoodsbatchs());//批次
                    storage.setStorageDate(stock.getProducetime());//生产日期
                    storage.setRecommendBatchs(stock.getPutTime());//推荐批次
                    storage.setStoragePostionNumber(stock.getPositionNumber());//库位编码
                    storage.setStorageNumber(String.valueOf(outNumber));//出库数量为拣货单数量
                    storage.setStoragePutbalenum(String.valueOf(outNumber));//出库数量为拣货单大包数
                    storage.setStorageWeight(String.valueOf(outNumber.multiply(w)));//拣货单总重量
                    storage.setStorageVolume(String.valueOf(outNumber.multiply(v)));//拣货单总体积
                    storage.setStorageMoney(String.valueOf(u.multiply(outNumber)));
                    storage.setStockId(stockId);//库存id
                    storage.setPickingStatus(DateUtil.getDateS());
                    storage.setCurrentOperator(userService.getcurrentUser().getAccount());
                    solist.add(storage);

                    BigDecimal outNumber1	=  outNumber.subtract(putNum);
                    outNumber = outNumber1;//减 出库数量
                    srls++;
                } else {
                    stockId = Long.parseLong(stock.getId().toString());//库存id
                    //完善 拣货单明细
                    storage.setTrayNumber(stock.getTrayNumber());//托盘编号
                    storage.setStorageBatchnumber(stock.getGoodsbatchs());//批次
                    storage.setStorageDate(stock.getProducetime());//生产日期
                    storage.setRecommendBatchs(stock.getPutTime());//推荐批次
                    storage.setStoragePostionNumber(stock.getPositionNumber());//库位编码
                    storage.setStorageNumber(String.valueOf(outNumber));//出库数量为拣货单数量
                    storage.setStoragePutbalenum(String.valueOf(outNumber));//出库数量为拣货单大包数
                    storage.setStorageWeight(String.valueOf(outNumber.multiply(w)));//拣货单总重量
                    storage.setStorageVolume(String.valueOf(outNumber.multiply(v)));//拣货单总体积
                    storage.setStorageMoney(String.valueOf(u.multiply(outNumber)));
                    storage.setStockId(stockId);//库存id
                    storage.setPickingStatus(DateUtil.getDateS());
                    storage.setCurrentOperator(userService.getcurrentUser().getAccount());
                    solist.add(storage);

                    BigDecimal outNumber1	=  outNumber.subtract(putNum);
                    outNumber = outNumber1;//减 出库数量
                    srls++;
                }
            }else{
                //这托出不完
                stockId = Long.parseLong(stock.getId().toString());//库存id

                BigDecimal outNumber1	=  outNumber.subtract(putNum);
                outNumber = outNumber1;//减 出库数量

                //完善 拣货单明细
                storage.setTrayNumber(stock.getTrayNumber());
                storage.setStorageBatchnumber(stock.getGoodsbatchs());//批次
                storage.setStorageDate(stock.getProducetime());//生产日期
                storage.setRecommendBatchs(stock.getPutTime());//推荐批次
                storage.setStoragePostionNumber(stock.getPositionNumber());
                storage.setStoragePutbalenum(String.valueOf(stock.getBalenum()));
                storage.setStorageNumber(String.valueOf(stock.getNum()));
                storage.setStorageWeight(String.valueOf(stock.getStockWeight()));
                storage.setStorageVolume(String.valueOf(stock.getStockVolume()));
                storage.setStorageMoney(String.valueOf(u.multiply(putNum)));
                storage.setStockId(stockId);//库存id
                storage.setPickingStatus(DateUtil.getDateS());
                storage.setCurrentOperator(userService.getcurrentUser().getAccount());
                solist.add(storage);
                srls++;
            }
            stock = new StorageStockResult();
            storage = new StorageOutDetailed();
        }
        if ( solist.size() > 0) {
            this.saveBatchList(solist);
        }
        StorageOutDetailedParam detailedParam2 = new StorageOutDetailedParam();

        ToolUtil.copyProperties(detailedParam,detailedParam2);

        this.delete(detailedParam2);
        return this.findListBySpec(param);
    }

    @Override
    public String findnumberOfLocation(String[] storagePostionNumber, String storageGoodscode, String storageOutNumber) {
        return this.baseMapper.findnumberOfLocation(storagePostionNumber, storageGoodscode,  storageOutNumber);
    }

    @Override
    public List<StorageOutDetailedResult> sumfindListBySpec(StorageOutDetailedParam param) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<StorageOutDetailedResult> findalreadyListBySpec(StorageOutDetailedParam param,String[] strs) {
        return this.baseMapper.findalreadyListBySpec(param,strs);
    }

    @Override
    public StorageOutDetailedResult findSumBySpec(StorageOutDetailedParam param, Long outStorehouseId) {
        return this.baseMapper.findSumBySpec(param,outStorehouseId);
    }

    @Override
    public List<StorageOutDetailedResult> findListBymaekSpec(StorageOutDetailedParam param, String mark) {
        // TODO Auto-generated method stub
        return this.baseMapper.findListBymaekSpec(param,mark);
    }

    @Override
    public List<StorageOutDetailedResult> findTotalalready(StorageOutDetailedParam param) {
        return this.baseMapper.findTotalalready(param);
    }

    @Override
    public void deleteBySpec(StorageOutDetailedParam param) {
        this.baseMapper.deleteBySpec(param);
    }

    @Override
    public void updateBySpec(StorageOutDetailedParam param) {
        this.baseMapper.updateBySpec(param);
    }

    @Override
    public List<StorageOutDetailedResult> findByStorageOutNumbersPageNew(String[] strs) {
        return this.baseMapper.findByStorageOutNumbersPageNew(strs);
    }

    @Override
    public StorageOutDetailedResult monthOutCount(StorageOutDetailedParam param, Long warehouseId) {
        return this.baseMapper.monthOutCount(param,warehouseId);
    }

    @Override
    public List<StorageOutDetailedResult> monthOutGoodsCount(StorageOutDetailedParam param,
                                                             Long warehouseId) {
        return this.baseMapper.monthOutGoodsCount(param,warehouseId);
    }

    @Override
    public List<StorageOutDetailedResult> appMonthJianxuanCount(StorageOutDetailedParam param,
                                                                Long warehouseId) {
        return this.baseMapper.appMonthJianxuanCount(param,warehouseId);
    }

    @Override
    public List<StorageOutDetailedResult> wmsData(StorageOutDetailedParam param, Long warehouseId) {
        return this.baseMapper.wmsData(param,warehouseId);
    }

    @Override
    public StorageOutDetailedResult sumByStockId(StorageOutDetailedParam param) {
        return this.baseMapper.sumByStockId(param);
    }

    @Override
    public StorageOutDetailedResult sumCountAndYijianshu(String[] strs) {
        return this.baseMapper.sumCountAndYijianshu(strs);
    }

    @Override
    public LayuiPageInfo findPageBySpecChukujilu(StorageOutDetailedParam param,String condition,Long outStorehouseId, Long deptId, Long deliverId,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findPageBySpecChukujilu(pageContext, param, condition, outStorehouseId, deptId, deliverId, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<StorageOutDetailedResult> findjianxuanzuoyehuizong(StorageOutDetailedParam storageOutDetailedParam,
                                                                   String condition, Long outStorehouseId, Long deptId, Long deliverId,Long operateId) {
        return this.baseMapper.findjianxuanzuoyehuizong( storageOutDetailedParam,
                condition,  outStorehouseId,  deptId,  deliverId,operateId);
    }

    @Override
    public List<StorageOutDetailedResult> findjianxuanzuoyehuizongRi(StorageOutDetailedParam storageOutDetailedParam,
                                                                     String condition, Long outStorehouseId, Long deptId, Long deliverId,Long operateId) {
        return this.baseMapper.findjianxuanzuoyehuizongRi( storageOutDetailedParam,
                condition,  outStorehouseId,  deptId,  deliverId, operateId);
    }

    @Override
    public List<StorageOutDetailedResult> findhuopinchukulianghuizong(StorageOutDetailedParam storageOutDetailedParam,
                                                                      String condition, Long outStorehouseId, Long deptId, Long deliverId,Long operateId) {
        return this.baseMapper.findhuopinchukulianghuizong( storageOutDetailedParam,
                condition,  outStorehouseId,  deptId,  deliverId,operateId);
    }


    @Override
    public List<StorageOutDetailedResult> findgoodsbystrs(StorageOutDetailedParam storageOutDetailedParam,
                                                          String[] strs) {
        return this.baseMapper.findgoodsbystrs(storageOutDetailedParam,strs);
    }

    @Override
    public List<StorageOutDetailedResult> findListStockIdBySpec(StorageOutDetailedParam param) {
        return this.baseMapper.findListStockIdBySpec(param);
    }

    @Override
    public List<StorageOutDetailedResult> trayInformationNew(StorageOutDetailedParam storageOutDetailedParam,
                                                             String[] strs) {
        return this.baseMapper.trayInformationNew(storageOutDetailedParam,strs);
    }

    @Override
    public List<StorageOutDetailedResult> findByStorageOutNumbersPageNewS(StorageOutDetailedParam param2,
                                                                          String[] strs) {
        return this.baseMapper.findByStorageOutNumbersPageNewS(param2,strs);
    }

    @Override
    public List<StorageOutDetailedResult> trayInformationNewBymark(StorageOutDetailedParam param3, String[] strs) {
        return this.baseMapper.trayInformationNewBymark(param3,strs);
    }

    @Override
    public List<StorageOutDetailedResult> findListBySpecandstrs(StorageOutDetailedParam param2, String[] strs) {
        return this.baseMapper.findListBySpecandstrs(param2,strs);
    }

    @Override
    public StorageOutDetailedResult sumBySpecandstrs(StorageOutDetailedParam param2, String[] strs) {
        return this.baseMapper.sumBySpecandstrs(param2,strs);
    }

    @Override
    public StorageOutDetailedResult findtrayInformationNewBymark(StorageOutDetailedParam param3, String[] strs) {
        return this.baseMapper.findtrayInformationNewBymark(param3,strs);
    }

    @Override
    public List<StorageOutDetailedResult> findByStorageOutNumbersPageNewexamine(String[] strs) {
        return this.baseMapper.findByStorageOutNumbersPageNewexamine(strs);
    }

    @Override
    public List<StorageOutDetailedResult> trayInformationNewBymarkexamine(StorageOutDetailedParam param3,
                                                                          String[] strs) {
        return this.baseMapper.trayInformationNewBymarkexamine(param3,strs);
    }

    @Override
    public List<StorageOutDetailedResult> trayInformationNewexamine(StorageOutDetailedParam param3, String[] strs) {
        return this.baseMapper.trayInformationNewexamine(param3,strs);
    }

    @Override
    public List<StorageOutDetailedResult> findjianhuomingxi(StorageOutDetailedParam storageOutDetailedParam,
                                                            String[] strs) {
        return this.baseMapper.findjianhuomingxi(storageOutDetailedParam,strs);
    }

    @Override
    public List<StorageOutDetailedResult> findjianhuomingxiBymake(StorageOutDetailedParam storageOutDetailedParam,
                                                                  String[] strs) {
        return this.baseMapper.findjianhuomingxiBymake(storageOutDetailedParam,strs);
    }

    @Override
    public List<StorageOutDetailedResult> findjianhuomingxiBymakeexamine(
            StorageOutDetailedParam storageOutDetailedParam, String[] strs) {
        return this.baseMapper.findjianhuomingxiBymakeexamine(storageOutDetailedParam,strs);
    }

    @Override
    public List<StorageOutDetailedResult> findjianhuomingxiexamine(StorageOutDetailedParam storageOutDetailedParam,
                                                                   String[] strs) {
        return this.baseMapper.findjianhuomingxiexamine(storageOutDetailedParam,strs);
    }

    @Resource
    private UserAop userAop;

    @Override
    public ResponseData confirmpickingbatch(List<StorageOutDetailedResult> listss,User user) {
        ResponseData data = new ResponseData();
        for (StorageOutDetailedResult rrr : listss) {
            if(!rrr.getStorageOutStatus().equals("PENDING_PICKING")) {
                continue;
            }
            StorageOutDetailedParam param = new StorageOutDetailedParam();
            StorageOutDetailed  storageOutDetailed = this.getById(rrr.getId());//拣货明细
            StorageStock storageStock = storageStockService.getById(storageOutDetailed.getStockId());//托盘

            BasicCustomer basicCustomer = basicCustomerService.getById(storageStock.getCustomerId());

            StorageOutParam outParam2 = new StorageOutParam();
            outParam2.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
            StorageOutResult outResult2 = storageOutService.findBySpec(outParam2);//拣货单


            StorageOutDetailedParam param3 = new StorageOutDetailedParam();
            param3.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
            param3.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());
            param3.setStorageBatchnumber(storageOutDetailed.getStorageBatchnumber());
            param3.setStorageOutStatus("PENDING_PICKING");

            List<StorageOutDetailedResult> list = new ArrayList<StorageOutDetailedResult>();
            if (!"PENDING_PICKING".equals(storageOutDetailed.getStorageOutStatus())) {
                return ResponseData.success();
            }

            BigDecimal numinpt = new BigDecimal(rrr.getStorageNumber());//拣货数
            BigDecimal storageNumber = new BigDecimal(storageOutDetailed.getStorageNumber()) ;//明细数量
            boolean falg = false;
            if (numinpt.compareTo(storageNumber)==1) {//拣超
                data.setSuccess(false);
                data.setMessage("Over-picking not allowed!");
                return data;
            }

            BigDecimal as = new BigDecimal(0);
            boolean asd = false;//少拣标记
            if (numinpt.compareTo(storageNumber)==-1) {//少拣
                asd = true;
                if (numinpt.compareTo(as)==0) {
                    data.setSuccess(false);
                    data.setMessage("Cannot be zero!");
                    return data;
                }
            }

            if (storageStock!=null) {
                if(storageStock.getStockstate().equals("IN_STOCK")) {
                    BigDecimal snum = new BigDecimal(storageStock.getNum());//托盘数
                    if (snum.compareTo(numinpt)==1) {//库存 大于 拣货
                        StorageStockParam param2 = new StorageStockParam();
                        BasicGoods basicGoods = basicGoodsService.getById(storageStock.getGoodsId());
                        BigDecimal u = new BigDecimal(1);
                        if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
                            u = new BigDecimal(basicGoods.getGoodsCharging());
                        }
                        BigDecimal v = new BigDecimal(1);
                        if(ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
                            v = new BigDecimal(basicGoods.getGoodsVolume());
                        }
                        BigDecimal w = new BigDecimal(1);
                        if(ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
                            w = new BigDecimal(basicGoods.getGoodsWeight());
                        }
                        BigDecimal less = snum.subtract(numinpt);
                        storageStock.setNum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        storageStock.setCanNum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        storageStock.setBalenum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        storageStock.setStockWeight(less.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        storageStock.setStockVolume(less.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        storageStock.setStockMoney(less.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        ToolUtil.copyProperties(storageStock,param2);
                        param2.setCreateTime(new Date());
                        param2.setCreateUser(user.getUserId());
                        param2.setUpdateTime(null);
                        param2.setUpdateUser(null);
                        storageStockService.update(param2);//修改 库存信息

                        param2.setStockId(Long.parseLong(param2.getId().toString()));
                        param2.setId(null);
                        param2.setNum(numinpt.toString());
                        param2.setBalenum(numinpt.toString());
                        param2.setCanNum(numinpt.toString());
                        param2.setStockWeight(numinpt.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        param2.setStockVolume(numinpt.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        param2.setStockMoney(numinpt.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        param2.setStockstate("PENDING_SHIPMENT");
                        storageStockService.addReturnId(param2);//生成 待出库库存

                        storageOutDetailed.setStockId(Long.parseLong(param2.getId().toString()));
                    } else if (snum.compareTo(numinpt)==0){//库存 等于 拣货
                        StorageStockParam param2 = new StorageStockParam();
                        param2.setId(storageStock.getId());
                        param2.setStockstate("PENDING_SHIPMENT");
                        storageStockService.update(param2);//修改 库存为待出库
                    } else if (snum.compareTo(numinpt)==-1) {//库存 小于 拣货
                        falg = true;
                    }
                } else {
                    falg = true;
                }
            } else {
                falg = true;
            }
            if (falg) {
                StorageStockParam stockParam = new StorageStockParam();
                OutOrderDetailedParam outOrderDetailedParam = new OutOrderDetailedParam();//查询是否指定 批次 出库
                outOrderDetailedParam.setDetailedOutNumber(outResult2.getOutOrderNumber());
                outOrderDetailedParam.setDetailedGoodsId(Long.parseLong(storageOutDetailed.getStorageGoodsId().toString()));
                OutOrderDetailedResult outOrderDetailedResult =  outOrderDetailedService.findBySpec(outOrderDetailedParam);
                if(outOrderDetailedResult != null) {
                    if(ToolUtil.isNotEmpty(outOrderDetailedResult.getDetailedBatchnumber()) && ToolUtil.isNotEmpty(basicCustomer.getGoodsBatchFlag()) && "Yes".equals(basicCustomer.getGoodsBatchFlag()) ) {
                        stockParam.setGoodsbatchs(storageOutDetailed.getStorageBatchnumber());
                    }
                }
                stockParam.setWarehouseId(outResult2.getOutStorehouseId());
                stockParam.setGoodsId(Long.parseLong(storageOutDetailed.getStorageGoodsId().toString()));
                stockParam.setStockstate("IN_STOCK");
                StorageStockResult rStockResult = storageStockService.sumbysqzkk(stockParam);

                if (rStockResult!=null) {
                    BigDecimal numrrr = new BigDecimal(rStockResult.getNum());//当前库存
                    if (numinpt.compareTo(numrrr)==1) {
                        data.setSuccess(false);
                        data.setMessage(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                        return data;
                    } else {
                        StorageOutDetailedResult result = new StorageOutDetailedResult();
                        ToolUtil.copyProperties(storageOutDetailed,result);
                        data.setSuccess(true);
                        data.setCode(405);
                        data.setData(this.completeNew(result,user));
                        data.setMessage("Pallet quantity has changed! Reprocessing completed!");
                        return data;
                    }
                }

                data.setSuccess(false);
                data.setMessage(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                return data;
            }

            if (asd) {
                BasicGoods basicGoods = basicGoodsService.getById(storageOutDetailed.getStorageGoodsId());
                BigDecimal u = new BigDecimal(1);
                if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
                    u = new BigDecimal(basicGoods.getGoodsCharging());
                }
                BigDecimal v = new BigDecimal(1);
                if(ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
                    v = new BigDecimal(basicGoods.getGoodsVolume());
                }
                BigDecimal w = new BigDecimal(1);
                if(ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
                    w = new BigDecimal(basicGoods.getGoodsWeight());
                }
                param.setId(storageOutDetailed.getId());
                param.setStorageOutStatus("PICKED");
                param.setStorageNumber(numinpt.toString());
                param.setRealNum(numinpt.toString());
                param.setStoragePutbalenum(numinpt.toString());//出库数量为拣货单大包数
                param.setStoragePrice(u.toString());
                param.setStorageWeight(String.valueOf(numinpt.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总重量
                param.setStorageVolume(String.valueOf(numinpt.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总体积
                param.setStorageMoney(String.valueOf(u.multiply(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                param.setStorageOutUser(user.getUserId());//操作人user
                param.setStorageOutName(user.getName());//操作人
                param.setSigns("F");
                this.update(param);

                //生成少拣  拣货明细
                BigDecimal less2 = storageNumber.subtract(numinpt);
                StorageOutDetailedParam detailedParam = new StorageOutDetailedParam();
                detailedParam.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());//拣货单编号
                detailedParam.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());//商品编号
                detailedParam.setStorageGoodsId(storageOutDetailed.getStorageGoodsId());
                detailedParam.setStorageGoodsname(storageOutDetailed.getStorageGoodsname());
                detailedParam.setStorageNumber(less2.toString());//出库数
                detailedParam.setStoragePutbalenum(less2.toString());
                detailedParam.setStorageBatchnumber(storageOutDetailed.getStorageBatchnumber());
                detailedParam.setStorageDate(storageOutDetailed.getStorageDate());
                detailedParam.setStorageOutStatus("PENDING_PICKING");
                detailedParam.setTrayNumber(storageOutDetailed.getTrayNumber());
                detailedParam.setStoragePostionNumber(storageOutDetailed.getStoragePostionNumber());
                detailedParam.setGoodsUnit(storageOutDetailed.getGoodsUnit());
                detailedParam.setGoodsSpecs(storageOutDetailed.getGoodsSpecs());
                String a = storageStockService.matching(detailedParam);//匹配库位
                if ("n".equals(a)) {
                    data.setSuccess(false);
                    data.setMessage(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                    return data;
                }
            } else {
                StorageOutDetailedParam updateDetailedParam = new StorageOutDetailedParam();
                updateDetailedParam.setId(storageOutDetailed.getId());
                updateDetailedParam.setStockId(storageOutDetailed.getStockId());
                updateDetailedParam.setStorageOutStatus("PICKED");
                updateDetailedParam.setRealNum(numinpt.toString());
                updateDetailedParam.setStorageOutUser(user.getUserId());//操作人user
                updateDetailedParam.setStorageOutName(user.getName());//操作人
                this.update(updateDetailedParam);

                StorageOutDetailedParam p = new StorageOutDetailedParam();//校验是否全部拣完
                p.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
                p.setStorageOutStatus("PENDING_PICKING");
                if (ToolUtil.isNotEmpty(outResult2.getMark())) {
                    list = this.findListBymaekSpec(p,outResult2.getMark());
                } else {
                    list = this.findListBySpec(p);
                }
                if (list.size()==0) {
                    List<StorageOutResult> olist = new ArrayList<StorageOutResult>();
                    if (ToolUtil.isNotEmpty(outResult2.getMark())) {
                        StorageOutParam outParam = new StorageOutParam();
                        outParam.setMark(outResult2.getMark());
                        olist = storageOutService.findListByMakeSpec(outParam);
                    } else {
                        olist.add(outResult2);
                    }

                    for(StorageOutResult outResult : olist ) {
                        StorageOutParam outParam = new StorageOutParam();
                        outResult.setOutOrderStatus("PENDING_APPROVAL");

                        ToolUtil.copyProperties(outResult,outParam);
                        storageOutService.update(outParam);
                    }
                }
            }
        }
        return ResponseData.success();
    }

    /**
     * 确认拣货
     */
    @Override
    public ResponseData confirmpicking(StorageOutDetailedParam param, String num) {
        User user = userAop.getUser();
        ResponseData data = new ResponseData();
        StorageOutDetailed  storageOutDetailed = this.getById(param.getId());//拣货明细
        StorageStock storageStock = storageStockService.getById(storageOutDetailed.getStockId());//托盘
        //获取客户信息
        BasicCustomer basicCustomer = basicCustomerService.getById(storageStock.getCustomerId());

        StorageOutParam outParam2 = new StorageOutParam();
        outParam2.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
        StorageOutResult outResult2 = storageOutService.findBySpec(outParam2);//拣货单

        StorageOutDetailedParam param3 = new StorageOutDetailedParam();
        param3.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
        param3.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());
        param3.setStorageBatchnumber(storageOutDetailed.getStorageBatchnumber());
        param3.setStorageOutStatus("PENDING_PICKING");

        List<StorageOutDetailedResult> list = new ArrayList<StorageOutDetailedResult>();
        if (!"PENDING_PICKING".equals(storageOutDetailed.getStorageOutStatus())) {
            return ResponseData.success();
        }

        BigDecimal numinpt = new BigDecimal(num);//拣货数
        BigDecimal storageNumber = new BigDecimal(storageOutDetailed.getStorageNumber()) ;//明细数量
        boolean falg = false;
        if (numinpt.compareTo(storageNumber)==1) {//拣超
            data.setSuccess(false);
            data.setMessage("Over-picking not allowed!");
            return data;
        }

        BigDecimal as = new BigDecimal(0);
        boolean asd = false;//少拣标记
        if (numinpt.compareTo(storageNumber)==-1) {//少拣
            asd = true;
            if (numinpt.compareTo(as)==0) {
                data.setSuccess(false);
                data.setMessage("Cannot be zero!");
                return data;
            }
        }

        if (storageStock!=null) {
            if(storageStock.getStockstate().equals("IN_STOCK")) {
                BigDecimal snum = new BigDecimal(storageStock.getNum());//托盘数
                if (snum.compareTo(numinpt)==1) {//库存 大于 拣货
                    StorageStockParam param2 = new StorageStockParam();
                    BasicGoods basicGoods = basicGoodsService.getById(storageStock.getGoodsId());
                    BigDecimal u = new BigDecimal(1);
                    if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
                        u = new BigDecimal(basicGoods.getGoodsCharging());
                    }
                    BigDecimal v = new BigDecimal(1);
                    if(ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
                        v = new BigDecimal(basicGoods.getGoodsVolume());
                    }
                    BigDecimal w = new BigDecimal(1);
                    if(ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
                        w = new BigDecimal(basicGoods.getGoodsWeight());
                    }
                    BigDecimal less = snum.subtract(numinpt);
                    storageStock.setNum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    storageStock.setCanNum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    storageStock.setBalenum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    storageStock.setStockWeight(less.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    storageStock.setStockVolume(less.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    storageStock.setStockMoney(less.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    ToolUtil.copyProperties(storageStock,param2);
                    param2.setCreateTime(new Date());
                    param2.setCreateUser(user.getUserId());
                    param2.setUpdateTime(null);
                    param2.setUpdateUser(null);
                    storageStockService.update(param2);//修改 库存信息

                    param2.setStockId(Long.parseLong(param2.getId().toString()));
                    param2.setId(null);
                    param2.setNum(numinpt.toString());
                    param2.setBalenum(numinpt.toString());
                    param2.setCanNum(numinpt.toString());
                    param2.setStockWeight(numinpt.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    param2.setStockVolume(numinpt.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    param2.setStockMoney(numinpt.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    param2.setStockstate("PENDING_SHIPMENT");
                    storageStockService.addReturnId(param2);//生成 待出库库存

                    storageOutDetailed.setStockId(Long.parseLong(param2.getId().toString()));
                } else if (snum.compareTo(numinpt)==0){//库存 等于 拣货
                    StorageStockParam param2 = new StorageStockParam();
                    param2.setStockstate("PENDING_SHIPMENT");
                    param2.setId(storageStock.getId());
                    storageStockService.update(param2);//修改 库存为待出库
                } else if (snum.compareTo(numinpt)==-1) {//库存 小于 拣货
                    falg = true;
                }
            } else {
                falg = true;
            }
        } else {
            falg = true;
        }
        if (falg) {
            StorageStockParam stockParam = new StorageStockParam();
            OutOrderDetailedParam outOrderDetailedParam = new OutOrderDetailedParam();//查询是否指定 批次 出库
            outOrderDetailedParam.setDetailedOutNumber(outResult2.getOutOrderNumber());
            outOrderDetailedParam.setDetailedGoodsId(Long.parseLong(storageOutDetailed.getStorageGoodsId().toString()));
            OutOrderDetailedResult outOrderDetailedResult =  outOrderDetailedService.findBySpec(outOrderDetailedParam);
            if(outOrderDetailedResult != null) {
                if(ToolUtil.isNotEmpty(outOrderDetailedResult.getDetailedBatchnumber()) && ToolUtil.isNotEmpty(basicCustomer.getGoodsBatchFlag()) && "Yes".equals(basicCustomer.getGoodsBatchFlag())) {
                    stockParam.setGoodsbatchs(storageOutDetailed.getStorageBatchnumber());
                }
            }
            stockParam.setWarehouseId(outResult2.getOutStorehouseId());
            stockParam.setGoodsId(Long.parseLong(storageOutDetailed.getStorageGoodsId().toString()));
            stockParam.setStockstate("IN_STOCK");
            StorageStockResult rStockResult = storageStockService.sumbysqzkk(stockParam);

            if (rStockResult!=null) {
                BigDecimal numrrr = new BigDecimal(rStockResult.getNum());//当前库存
                if (numinpt.compareTo(numrrr)==1) {
                    data.setSuccess(false);
                    data.setMessage(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                    return data;
                } else {
                    StorageOutDetailedResult result = new StorageOutDetailedResult();
                    ToolUtil.copyProperties(storageOutDetailed,result);
                    data.setSuccess(true);
                    data.setCode(405);
                    data.setData(this.completeNew(result,user));
                    data.setMessage("Pallet quantity has changed! Reprocessing completed!");
                    return data;
                }
            }

            data.setSuccess(false);
            data.setMessage(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
            return data;
        }

        if (asd) {
            BasicGoods basicGoods = basicGoodsService.getById(storageStock.getGoodsId());
            BigDecimal u = new BigDecimal(1);
            if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
                u = new BigDecimal(basicGoods.getGoodsCharging());
            }
            BigDecimal v = new BigDecimal(1);
            if(ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
                v = new BigDecimal(basicGoods.getGoodsVolume());
            }
            BigDecimal w = new BigDecimal(1);
            if(ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
                w = new BigDecimal(basicGoods.getGoodsWeight());
            }
            param.setStockId(storageOutDetailed.getStockId());
            param.setStorageOutStatus("PICKED");
            param.setStorageNumber(numinpt.toString());
            param.setRealNum(numinpt.toString());
            param.setStoragePutbalenum(numinpt.toString());
            param.setStorageWeight(numinpt.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
            param.setStorageVolume(numinpt.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
            param.setStoragePrice(u.toString());
            param.setStorageMoney(numinpt.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
            param.setStorageOutUser(user.getUserId());//操作人user
            param.setStorageOutName(user.getName());//操作人
            param.setSigns("N");
            this.update(param);

            //生成少拣  拣货明细
            BigDecimal less2 = storageNumber.subtract(numinpt);
            StorageOutDetailedParam detailedParam = new StorageOutDetailedParam();
            detailedParam.setStorageBatchnumber(storageOutDetailed.getStorageBatchnumber());
            detailedParam.setStorageDate(storageOutDetailed.getStorageDate());
            detailedParam.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());//拣货单编号
            detailedParam.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());//商品编号
            detailedParam.setStorageGoodsId(storageOutDetailed.getStorageGoodsId());
            detailedParam.setStorageGoodsname(storageOutDetailed.getStorageGoodsname());
            detailedParam.setStorageNumber(less2.toString());//出库数
            detailedParam.setStoragePutbalenum(less2.toString());
            detailedParam.setStorageOutStatus("PENDING_PICKING");
            detailedParam.setTrayNumber(storageOutDetailed.getTrayNumber());
            detailedParam.setStoragePostionNumber(storageOutDetailed.getStoragePostionNumber());
            detailedParam.setGoodsUnit(storageOutDetailed.getGoodsUnit());
            detailedParam.setGoodsSpecs(storageOutDetailed.getGoodsSpecs());
            String a = storageStockService.matching(detailedParam);//匹配库位
            if ("n".equals(a)) {
                data.setSuccess(false);
                data.setMessage(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                return data;
            }
        } else {
            StorageOutDetailedParam updateDetailedParam = new StorageOutDetailedParam();
            updateDetailedParam.setId(storageOutDetailed.getId());
            updateDetailedParam.setStockId(storageOutDetailed.getStockId());
            updateDetailedParam.setStorageOutStatus("PICKED");
            updateDetailedParam.setRealNum(numinpt.toString());
            updateDetailedParam.setStorageOutUser(user.getUserId());//操作人user
            updateDetailedParam.setStorageOutName(user.getName());//操作人
            updateDetailedParam.setSigns("O");
            this.update(updateDetailedParam);

            StorageOutDetailedParam p = new StorageOutDetailedParam();//校验是否全部拣完
            p.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
            p.setStorageOutStatus("PENDING_PICKING");
            if (ToolUtil.isNotEmpty(outResult2.getMark())) {
                list = this.findListBymaekSpec(p,outResult2.getMark());
            } else {
                list = this.findListBySpec(p);
            }
            if (list.size()==0) {
                List<StorageOutResult> olist = new ArrayList<StorageOutResult>();
                if (ToolUtil.isNotEmpty(outResult2.getMark())) {
                    StorageOutParam outParam = new StorageOutParam();
                    outParam.setMark(outResult2.getMark());
                    olist = storageOutService.findListByMakeSpec(outParam);
                } else {
                    olist.add(outResult2);
                }

                for(StorageOutResult outResult : olist ) {
                    StorageOutParam outParam = new StorageOutParam();
                    outParam.setId(outResult.getId());
                    outParam.setOutOrderStatus("PENDING_APPROVAL");
                    storageOutService.update(outParam);
                }
            } else {
                p.setStorageGoodsId(storageOutDetailed.getStorageGoodsId());
                if (ToolUtil.isNotEmpty(outResult2.getMark())) {
                    list = this.findListBymaekSpec(p,outResult2.getMark());
                } else {
                    list = this.findListBySpec(p);
                }
                if(list.size()>0) {
                    return ResponseData.success(this.completeNew(list.get(0), user));
                }
            }
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData zhidingpicking(StorageStockParam sp, BigDecimal numinpt,StorageOutDetailed storageOutDetailed,User user,StorageOutResult outResult) {
        List<StorageStockResult> srl2 = storageStockService.finByOnlyOnePositionNumber(sp);
        List<StorageOutDetailedResult> list = new ArrayList<StorageOutDetailedResult>();
        BigDecimal storageNumber = new BigDecimal(storageOutDetailed.getStorageNumber()) ;//明细数量
        BigDecimal less2 = storageNumber.subtract(numinpt);
        if (numinpt.compareTo(storageNumber)==1) {//拣超
            return ResponseData.error("Over-picking not allowed!");
        }
        BigDecimal as = new BigDecimal(0);
        boolean asd = false;//少拣标记
        if (numinpt.compareTo(storageNumber)==-1) {//少拣
            asd = true;
            if (numinpt.compareTo(as)==0) {
                return ResponseData.error("Cannot be zero!");
            }
        }
        boolean breakflag = false;
        StorageOutDetailedParam param = new StorageOutDetailedParam();
        for (StorageStockResult r : srl2) {
            if(breakflag) {
                break;
            }
            BigDecimal snum = new BigDecimal(r.getNum());//托盘数
            if (snum.compareTo(numinpt)==1) {//库存 大于 拣货
                StorageStockParam param2 = new StorageStockParam();
                BasicGoods basicGoods = basicGoodsService.getById(r.getGoodsId());
                BigDecimal u = new BigDecimal(1);
                if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
                    u = new BigDecimal(basicGoods.getGoodsCharging());
                }
                BigDecimal v = new BigDecimal(1);
                if(ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
                    v = new BigDecimal(basicGoods.getGoodsVolume());
                }
                BigDecimal w = new BigDecimal(1);
                if(ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
                    w = new BigDecimal(basicGoods.getGoodsWeight());
                }
                BigDecimal less = snum.subtract(numinpt);
                r.setNum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                r.setCanNum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                r.setBalenum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                r.setStockWeight(less.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                r.setStockVolume(less.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                r.setStockMoney(less.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                ToolUtil.copyProperties(r,param2);
                param2.setCreateTime(new Date());
                param2.setCreateUser(user.getUserId());
                param2.setUpdateTime(null);
                param2.setUpdateUser(null);
                storageStockService.update(param2);//修改 库存信息

                param2.setStockId(Long.parseLong(param2.getId().toString()));
                param2.setId(null);
                param2.setNum(numinpt.toString());
                param2.setBalenum(numinpt.toString());
                param2.setCanNum(numinpt.toString());
                param2.setStockWeight(numinpt.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                param2.setStockVolume(numinpt.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                param2.setStockMoney(numinpt.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                param2.setStockstate("PENDING_SHIPMENT");
                storageStockService.addReturnId(param2);//生成 待出库库存

                storageOutDetailed.setStorageBatchnumber(param2.getGoodsbatchs());
                storageOutDetailed.setStorageDate(param2.getProducetime());
                storageOutDetailed.setTrayNumber(param2.getTrayNumber());
                storageOutDetailed.setStoragePostionNumber(param2.getPositionNumber());
                storageOutDetailed.setStoragePrice(param2.getUnitprice());
                storageOutDetailed.setStorageWeight(param2.getStockWeight());//拣货单总重量
                storageOutDetailed.setStorageVolume(param2.getStockVolume());//拣货单总体积
                storageOutDetailed.setStorageMoney(param2.getStockMoney());
                storageOutDetailed.setStockId(Long.parseLong(param2.getId().toString()));
                storageOutDetailed.setStorageOutStatus("PICKED");
                storageOutDetailed.setStoragePutbalenum(numinpt.toString());
                storageOutDetailed.setStorageNumber(numinpt.toString());
                storageOutDetailed.setRealNum(numinpt.toString());
                ToolUtil.copyProperties(storageOutDetailed,param );
                param.setStorageOutUser(user.getUserId());//操作人user
                param.setStorageOutName(user.getName());//操作人
                param.setSigns("P");
                this.update(param);
                numinpt = new BigDecimal(0);
                breakflag = true;
            }
            if (snum.compareTo(numinpt)==0) {//库存 等于 拣货
                StorageStockParam param2 = new StorageStockParam();
                param2.setStockstate("PENDING_SHIPMENT");
                param2.setId(r.getId());
                storageStockService.update(param2);//修改 库存为待出库

                storageOutDetailed.setStorageBatchnumber(r.getGoodsbatchs());
                storageOutDetailed.setStorageDate(r.getProducetime());
                storageOutDetailed.setTrayNumber(r.getTrayNumber());
                storageOutDetailed.setStoragePostionNumber(r.getPositionNumber());
                storageOutDetailed.setStoragePrice(r.getUnitprice());
                storageOutDetailed.setStorageWeight(r.getStockWeight());//拣货单总重量
                storageOutDetailed.setStorageVolume(r.getStockVolume());//拣货单总体积
                storageOutDetailed.setStorageMoney(r.getStockMoney());
                storageOutDetailed.setStockId(Long.parseLong(r.getId().toString()));
                storageOutDetailed.setStorageOutStatus("PICKED");
                storageOutDetailed.setStoragePutbalenum(numinpt.toString());
                storageOutDetailed.setStorageNumber(numinpt.toString());
                storageOutDetailed.setRealNum(numinpt.toString());
                ToolUtil.copyProperties(storageOutDetailed,param);
                param.setStorageOutUser(user.getUserId());//操作人user
                param.setStorageOutName(user.getName());//操作人
                param.setSigns("Q");
                this.update(param);
                numinpt = new BigDecimal(0);
                breakflag = true;
            }
            if (snum.compareTo(numinpt)==-1) {//库存 小于 拣货
                StorageStockParam param2 = new StorageStockParam();
                param2.setStockstate("PENDING_SHIPMENT");
                param2.setId(r.getId());
                storageStockService.update(param2);//修改 库存为待出库

                StorageOutDetailedParam detailedParam = new StorageOutDetailedParam();
                detailedParam.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());//拣货单编号
                detailedParam.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());//商品编号
                detailedParam.setStorageGoodsId(storageOutDetailed.getStorageGoodsId());
                detailedParam.setStorageGoodsname(storageOutDetailed.getStorageGoodsname());
                detailedParam.setStorageNumber(r.getNum());//出库数
                detailedParam.setRealNum(r.getNum());
                detailedParam.setStoragePutbalenum(r.getBalenum());
                detailedParam.setStorageBatchnumber(r.getGoodsbatchs());
                detailedParam.setStorageDate(r.getProducetime());
                detailedParam.setRecommendBatchs(r.getPutTime());
                detailedParam.setStorageOutStatus("PICKED");
                detailedParam.setTrayNumber(r.getTrayNumber());
                detailedParam.setStoragePostionNumber(r.getPositionNumber());
                detailedParam.setStoragePrice(r.getUnitprice());
                detailedParam.setStorageWeight(r.getStockWeight());//拣货单总重量
                detailedParam.setStorageVolume(r.getStockVolume());//拣货单总体积
                detailedParam.setStorageMoney(r.getStockMoney());
                detailedParam.setStockId(Long.parseLong(r.getId().toString()));//库存id
                detailedParam.setGoodsUnit(storageOutDetailed.getGoodsUnit());
                detailedParam.setGoodsSpecs(storageOutDetailed.getGoodsSpecs());
                detailedParam.setStorageOutUser(user.getUserId());//操作人user
                detailedParam.setStorageOutName(user.getName());//操作人
                detailedParam.setUpdateTime(new Date());
                detailedParam.setSigns("R");
                this.add(detailedParam);
                numinpt = numinpt.subtract(snum);
            }
        }
        if (asd) {
            if(numinpt.compareTo(as)==0) {
                //生成少拣  拣货明细
                StorageOutDetailedParam detailedParam = new StorageOutDetailedParam();
                detailedParam.setStorageBatchnumber(storageOutDetailed.getStorageBatchnumber());
                detailedParam.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());//拣货单编号
                detailedParam.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());//商品编号
                detailedParam.setStorageGoodsId(storageOutDetailed.getStorageGoodsId());
                detailedParam.setStorageGoodsname(storageOutDetailed.getStorageGoodsname());
                detailedParam.setStorageNumber(less2.toString());//出库数
                detailedParam.setStoragePutbalenum(less2.toString());
                detailedParam.setStorageOutStatus("PENDING_PICKING");
                detailedParam.setStoragePostionNumber(sp.getPositionNumber());
                detailedParam.setGoodsUnit(storageOutDetailed.getGoodsUnit());
                detailedParam.setGoodsSpecs(storageOutDetailed.getGoodsSpecs());
                String a = storageStockService.matching(detailedParam);//匹配库位
                if ("n".equals(a)) {
                    return ResponseData.error(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                }
            } else {
                param.setId(storageOutDetailed.getId());
                this.delete(param);
                less2 = less2.add(numinpt);
                //生成少拣  拣货明细
                StorageOutDetailedParam detailedParam = new StorageOutDetailedParam();
                detailedParam.setStorageBatchnumber(storageOutDetailed.getStorageBatchnumber());
                detailedParam.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());//拣货单编号
                detailedParam.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());//商品编号
                detailedParam.setStorageGoodsId(storageOutDetailed.getStorageGoodsId());
                detailedParam.setStorageGoodsname(storageOutDetailed.getStorageGoodsname());
                detailedParam.setStorageNumber(less2.toString());//出库数
                detailedParam.setStoragePutbalenum(less2.toString());
                detailedParam.setStorageOutStatus("PENDING_PICKING");
                detailedParam.setStoragePostionNumber(sp.getPositionNumber());
                detailedParam.setGoodsUnit(storageOutDetailed.getGoodsUnit());
                detailedParam.setGoodsSpecs(storageOutDetailed.getGoodsSpecs());
                String a = storageStockService.matching(detailedParam);//匹配库位
                if ("n".equals(a)) {
                    return ResponseData.error(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                }
            }
        } else {
            if(numinpt.compareTo(as)==1) {
                param.setId(storageOutDetailed.getId());
                this.delete(param);
                //生成少拣  拣货明细
                StorageOutDetailedParam detailedParam = new StorageOutDetailedParam();
                detailedParam.setStorageBatchnumber(storageOutDetailed.getStorageBatchnumber());
                detailedParam.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());//拣货单编号
                detailedParam.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());//商品编号
                detailedParam.setStorageGoodsId(storageOutDetailed.getStorageGoodsId());
                detailedParam.setStorageGoodsname(storageOutDetailed.getStorageGoodsname());
                detailedParam.setStorageNumber(numinpt.toString());//出库数
                detailedParam.setStoragePutbalenum(numinpt.toString());
                detailedParam.setStorageOutStatus("PENDING_PICKING");
                detailedParam.setStoragePostionNumber(sp.getPositionNumber());
                detailedParam.setGoodsUnit(storageOutDetailed.getGoodsUnit());
                detailedParam.setGoodsSpecs(storageOutDetailed.getGoodsSpecs());
                String a = storageStockService.matching(detailedParam);//匹配库位
                if ("n".equals(a)) {
                    return ResponseData.error(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                }
            }
        }
        StorageOutDetailedParam p = new StorageOutDetailedParam();//校验是否全部拣完
        p.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
        p.setStorageOutStatus("PENDING_PICKING");
        if (ToolUtil.isNotEmpty(outResult.getMark())) {
            list = this.findListBymaekSpec(p,outResult.getMark());
        } else {
            list = this.findListBySpec(p);
        }
        if (list.size()==0) {
            List<StorageOutResult> olist = new ArrayList<StorageOutResult>();
            if (ToolUtil.isNotEmpty(outResult.getMark())) {
                StorageOutParam outParam2 = new StorageOutParam();
                outParam2.setMark(outResult.getMark());
                olist = storageOutService.findListByMakeSpec(outParam2);
            } else {
                olist.add(outResult);
            }

            for(StorageOutResult outResult2 : olist ) {
                StorageOutParam outParam2 = new StorageOutParam();
                outParam2.setId(outResult2.getId());
                outParam2.setOutOrderStatus("PENDING_APPROVAL");
                storageOutService.update(outParam2);
            }
        } else {
            p.setStorageGoodsId(storageOutDetailed.getStorageGoodsId());
            if (ToolUtil.isNotEmpty(outResult.getMark())) {
                list = this.findListBymaekSpec(p,outResult.getMark());
            } else {
                list = this.findListBySpec(p);
            }
            if(list.size()>0) {
                return ResponseData.success(this.completeNew(list.get(0), user));
            }
        }
        return ResponseData.success();
    }

    /**
     * 合单拣货
     */
    @Override
    public ResponseData confirmpickingmake(StorageOutDetailedParam param, String num) {
        User user = userAop.getUser();
        ResponseData data = new ResponseData();

        String[] strs = param.getStorageOutNumber().split(",");
        StorageOutDetailedParam paramno1 = new StorageOutDetailedParam();
        paramno1.setStorageGoodsId(param.getStorageGoodsId());
        paramno1.setStoragePostionNumber(param.getStoragePostionNumber());
        paramno1.setTrayNumber(param.getTrayNumber());
        paramno1.setStorageBatchnumber(param.getStorageBatchnumber());
        paramno1.setStorageOutStatus("PENDING_PICKING");

        List<StorageOutDetailedResult> listno1 = this.findListBySpecandstrs(paramno1,strs);
        if(listno1.size()==0) {
            return ResponseData.success();
        }
        StorageOutDetailedResult storageOutDetailedResult = this.sumBySpecandstrs(paramno1,strs);
        if(storageOutDetailedResult == null) {
            return ResponseData.success();
        }
        BigDecimal as = new BigDecimal(0);
        BigDecimal iptno1 = new BigDecimal(num);//拣货数
        BigDecimal sumnum = new BigDecimal(storageOutDetailedResult.getStorageNumber());//总数
        if (iptno1.compareTo(sumnum)==1) {//拣超
            return ResponseData.error("Over-picking not allowed!");
        }
        if (iptno1.compareTo(as)==0) {
            return ResponseData.error("Cannot be zero!");
        }

        for (StorageOutDetailedResult storageOutDetailed : listno1) {
            if (iptno1.compareTo(as)==0) {
                break;
            }
            StorageStock storageStock = storageStockService.getById(storageOutDetailed.getStockId());//托盘
            //获取客户信息
            BasicCustomer basicCustomer = basicCustomerService.getById(storageStock.getCustomerId());

            StorageOutParam outParam2 = new StorageOutParam();
            outParam2.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
            StorageOutResult outResult2 = storageOutService.findBySpec(outParam2);//拣货单

            StorageOutDetailedParam param3 = new StorageOutDetailedParam();
            param3.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
            param3.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());
            param3.setStorageBatchnumber(storageOutDetailed.getStorageBatchnumber());
            param3.setStorageOutStatus("PENDING_PICKING");

            List<StorageOutDetailedResult> list = new ArrayList<StorageOutDetailedResult>();
            if (!"PENDING_PICKING".equals(storageOutDetailed.getStorageOutStatus())) {
                return ResponseData.success();
            }

            BigDecimal storageNumber = new BigDecimal(storageOutDetailed.getStorageNumber());//明细数量
            BigDecimal numinpt = new BigDecimal(0);//拣货数
            if(iptno1.compareTo(storageNumber) == 1) {
                numinpt = storageNumber;
                iptno1 = iptno1.subtract(storageNumber);
            } else if(iptno1.compareTo(storageNumber) == 0) {
                numinpt = storageNumber;
                iptno1 = as;
            } else if(iptno1.compareTo(storageNumber) == -1) {
                numinpt = iptno1;
                iptno1 = as;
            }

            boolean falg = false;
            boolean asd = false;//少拣标记
            if (numinpt.compareTo(storageNumber)==-1) {//少拣
                asd = true;
            }

            if (storageStock!=null) {
                if(storageStock.getStockstate().equals("IN_STOCK")) {
                    BigDecimal snum = new BigDecimal(storageStock.getNum());//库存数
                    if (snum.compareTo(numinpt)==1) {//库存 大于 拣货
                        StorageStockParam param2 = new StorageStockParam();
                        BasicGoods basicGoods = basicGoodsService.getById(storageStock.getGoodsId());
                        BigDecimal u = new BigDecimal(1);
                        if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
                            u = new BigDecimal(basicGoods.getGoodsCharging());
                        }
                        BigDecimal v = new BigDecimal(1);
                        if(ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
                            v = new BigDecimal(basicGoods.getGoodsVolume());
                        }
                        BigDecimal w = new BigDecimal(1);
                        if(ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
                            w = new BigDecimal(basicGoods.getGoodsWeight());
                        }
                        BigDecimal less = snum.subtract(numinpt);
                        storageStock.setNum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        storageStock.setCanNum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        storageStock.setBalenum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        storageStock.setStockWeight(less.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        storageStock.setStockVolume(less.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        storageStock.setStockMoney(less.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        ToolUtil.copyProperties(storageStock,param2);
                        param2.setCreateTime(new Date());
                        param2.setCreateUser(user.getUserId());
                        param2.setUpdateTime(null);
                        param2.setUpdateUser(null);
                        storageStockService.update(param2);//修改 库存信息

                        param2.setStockId(Long.parseLong(param2.getId().toString()));
                        param2.setId(null);
                        param2.setNum(numinpt.toString());
                        param2.setBalenum(numinpt.toString());
                        param2.setCanNum(numinpt.toString());
                        param2.setStockWeight(numinpt.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        param2.setStockVolume(numinpt.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        param2.setStockMoney(numinpt.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        param2.setStockstate("PENDING_SHIPMENT");
                        storageStockService.addReturnId(param2);//生成 待出库库存

                        storageOutDetailed.setStockId(Long.parseLong(param2.getId().toString()));
                    } else if (snum.compareTo(numinpt)==0){//库存 等于 拣货
                        StorageStockParam param2 = new StorageStockParam();
                        param2.setId(storageStock.getId());
                        param2.setStockstate("PENDING_SHIPMENT");
                        storageStockService.update(param2);//修改 库存为待出库
                    } else if (snum.compareTo(numinpt)==-1) {//库存 小于 拣货
                        falg = true;
                    }
                } else {
                    falg = true;
                }
            } else {
                falg = true;
            }
            if (falg) {
                StorageStockParam stockParam = new StorageStockParam();
                OutOrderDetailedParam outOrderDetailedParam = new OutOrderDetailedParam();//查询是否指定 批次 出库
                outOrderDetailedParam.setDetailedOutNumber(outResult2.getOutOrderNumber());
                outOrderDetailedParam.setDetailedGoodsId(Long.parseLong(storageOutDetailed.getStorageGoodsId().toString()));
                OutOrderDetailedResult outOrderDetailedResult =  outOrderDetailedService.findBySpec(outOrderDetailedParam);
                if(outOrderDetailedResult != null) {
                    if(ToolUtil.isNotEmpty(outOrderDetailedResult.getDetailedBatchnumber())   && ToolUtil.isNotEmpty(basicCustomer.getGoodsBatchFlag()) && "Yes".equals(basicCustomer.getGoodsBatchFlag())) {
                        stockParam.setGoodsbatchs(outOrderDetailedResult.getDetailedBatchnumber());
                    }
                }
                stockParam.setWarehouseId(outResult2.getOutStorehouseId());
                stockParam.setGoodsId(Long.parseLong(storageOutDetailed.getStorageGoodsId().toString()));
                stockParam.setStockstate("IN_STOCK");
                StorageStockResult rStockResult = storageStockService.sumbysqzkk(stockParam);

                if (rStockResult!=null) {
                    BigDecimal numrrr = new BigDecimal(rStockResult.getNum());//当前库存
                    if (numinpt.compareTo(numrrr)==1) {
                        data.setSuccess(false);
                        data.setMessage(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                        return data;
                    } else {
                        StorageOutDetailedResult result = new StorageOutDetailedResult();
                        ToolUtil.copyProperties(storageOutDetailed,result);
                        data.setSuccess(true);
                        data.setCode(405);
                        data.setData(this.completeNew(result,user));
                        data.setMessage("Pallet quantity has changed! Reprocessing completed!");
                        return data;
                    }
                }

                data.setSuccess(false);
                data.setMessage(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                return data;
            }

            if (asd) {
                BasicGoods basicGoods = basicGoodsService.getById(storageOutDetailed.getStorageGoodsId());
                BigDecimal u = new BigDecimal(1);
                if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
                    u = new BigDecimal(basicGoods.getGoodsCharging());
                }
                BigDecimal v = new BigDecimal(1);
                if(ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
                    v = new BigDecimal(basicGoods.getGoodsVolume());
                }
                BigDecimal w = new BigDecimal(1);
                if(ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
                    w = new BigDecimal(basicGoods.getGoodsWeight());
                }
                param.setId(storageOutDetailed.getId());
                param.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
                param.setStockId(storageOutDetailed.getStockId());
                param.setStorageOutStatus("PICKED");
                param.setStorageNumber(numinpt.toString());
                param.setRealNum(numinpt.toString());
                param.setStoragePutbalenum(numinpt.toString());//出库数量为拣货单大包数
                param.setStoragePrice(u.toString());
                param.setStorageWeight(String.valueOf(numinpt.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总重量
                param.setStorageVolume(String.valueOf(numinpt.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总体积
                param.setStorageMoney(String.valueOf(u.multiply(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                param.setStorageOutUser(user.getUserId());//操作人user
                param.setStorageOutName(user.getName());//操作人
                param.setSigns("T");
                this.update(param);

                if (asd) {
                    //生成少拣  拣货明细
                    BigDecimal less = storageNumber.subtract(numinpt);
                    StorageOutDetailedParam detailedParam = new StorageOutDetailedParam();
                    detailedParam.setStorageBatchnumber(storageOutDetailed.getStorageBatchnumber());
                    detailedParam.setStorageDate(storageOutDetailed.getStorageDate());
                    detailedParam.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());//拣货单编号
                    detailedParam.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());//商品编号
                    detailedParam.setStorageGoodsId(storageOutDetailed.getStorageGoodsId());
                    detailedParam.setStorageGoodsname(storageOutDetailed.getStorageGoodsname());
                    detailedParam.setStorageNumber(less.toString());//出库数
                    detailedParam.setStoragePutbalenum(less.toString());
                    detailedParam.setStorageOutStatus("PENDING_PICKING");
                    detailedParam.setTrayNumber(storageOutDetailed.getTrayNumber());
                    detailedParam.setStoragePostionNumber(storageOutDetailed.getStoragePostionNumber());
                    detailedParam.setPickingStatus(DateUtil.getDateS());
                    detailedParam.setCurrentOperator(user.getAccount());
                    detailedParam.setGoodsUnit(storageOutDetailed.getGoodsUnit());
                    detailedParam.setGoodsSpecs(storageOutDetailed.getGoodsSpecs());
                    String a = storageStockService.matching(detailedParam);//匹配库位
                    if ("n".equals(a)) {
                        data.setSuccess(false);
                        data.setMessage(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                        return data;
                    }
                }
            } else {
                StorageOutDetailedParam updateDetailedParam = new StorageOutDetailedParam();
                updateDetailedParam.setId(storageOutDetailed.getId());
                updateDetailedParam.setStorageOutStatus("PICKED");
                updateDetailedParam.setStockId(storageOutDetailed.getStockId());
                updateDetailedParam.setRealNum(numinpt.toString());
                updateDetailedParam.setStorageOutUser(user.getUserId());//操作人user
                updateDetailedParam.setStorageOutName(user.getName());//操作人
                updateDetailedParam.setSigns("U");
                this.update(updateDetailedParam);

                StorageOutDetailedParam p = new StorageOutDetailedParam();//校验是否全部拣完
                p.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
                p.setStorageOutStatus("PENDING_PICKING");
                if (ToolUtil.isNotEmpty(outResult2.getMark())) {
                    list = this.findListBymaekSpec(p,outResult2.getMark());
                } else {
                    list = this.findListBySpec(p);
                }
                if (list.size()==0) {
                    List<StorageOutResult> olist = new ArrayList<StorageOutResult>();
                    if (ToolUtil.isNotEmpty(outResult2.getMark())) {
                        StorageOutParam outParam = new StorageOutParam();
                        outParam.setMark(outResult2.getMark());
                        olist = storageOutService.findListByMakeSpec(outParam);
                    } else {
                        olist.add(outResult2);
                    }

                    for(StorageOutResult outResult : olist ) {
                        StorageOutParam outParam = new StorageOutParam();
                        outParam.setId(outResult.getId());
                        outParam.setOutOrderStatus("PENDING_APPROVAL");
                        storageOutService.update(outParam);
                    }
                }
            }
        }

        StorageOutDetailedParam param2 = new StorageOutDetailedParam();
        param2.setStorageGoodsId(param.getStorageGoodsId());
        param2.setStorageOutStatus("PENDING_PICKING");
        List<StorageOutDetailedResult> list = this.findListBySpecandstrs(param2,strs);
        for (StorageOutDetailedResult r : list) {
            this.completeNew(r,user);
        }
        List<StorageOutDetailedResult> sOutDR= new ArrayList<StorageOutDetailedResult>();
        StorageOutDetailedResult result = this.sumBySpecandstrs(param2,strs);
        if(result != null) {
            sOutDR.add(result);
        }
        return ResponseData.success(sOutDR);
    }

    @Override
    public ResponseData batchoutmake(List<StorageOutDetailedResult> list2, User user,StorageOutDetailedParam param) {
        ResponseData data = new ResponseData();
        String[] strs = param.getStorageOutNumber().split(",");
        for (StorageOutDetailedResult r : list2) {
            StorageOutDetailedParam paramno1 = new StorageOutDetailedParam();
            paramno1.setStorageGoodsId(r.getStorageGoodsId());
            paramno1.setStoragePostionNumber(r.getStoragePostionNumber());
            paramno1.setTrayNumber(r.getTrayNumber());
            paramno1.setStorageBatchnumber(r.getStorageBatchnumber());
            paramno1.setStorageOutStatus("PENDING_PICKING");

            List<StorageOutDetailedResult> listno1 = this.findListBySpecandstrs(paramno1,strs);
            if(listno1.size()==0) {
                continue;
            }
            StorageOutDetailedResult storageOutDetailedResult = this.sumBySpecandstrs(paramno1,strs);
            if(storageOutDetailedResult == null) {
                return ResponseData.success();
            }
            BigDecimal as = new BigDecimal(0);
            BigDecimal iptno1 = new BigDecimal(r.getStorageNumber());//拣货数

            for (StorageOutDetailedResult storageOutDetailed : listno1) {
                if (iptno1.compareTo(as)==0) {
                    break;
                }
                StorageStock storageStock = storageStockService.getById(storageOutDetailed.getStockId());//托盘
                //获取客户信息
                BasicCustomer basicCustomer = basicCustomerService.getById(storageStock.getCustomerId());
                StorageOutParam outParam2 = new StorageOutParam();
                outParam2.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
                StorageOutResult outResult2 = storageOutService.findBySpec(outParam2);//拣货单

                StorageOutDetailedParam param3 = new StorageOutDetailedParam();
                param3.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
                param3.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());
                param3.setStorageBatchnumber(storageOutDetailed.getStorageBatchnumber());
                param3.setStorageOutStatus("PENDING_PICKING");

                List<StorageOutDetailedResult> list = new ArrayList<StorageOutDetailedResult>();
                if (!"PENDING_PICKING".equals(storageOutDetailed.getStorageOutStatus())) {
                    return ResponseData.success();
                }

                BigDecimal storageNumber = new BigDecimal(storageOutDetailed.getStorageNumber());//明细数量
                BigDecimal numinpt = new BigDecimal(0);//拣货数
                if(iptno1.compareTo(storageNumber) == 1) {
                    numinpt = storageNumber;
                    iptno1 = iptno1.subtract(storageNumber);
                } else if(iptno1.compareTo(storageNumber) == 0) {
                    numinpt = storageNumber;
                    iptno1 = as;
                } else if(iptno1.compareTo(storageNumber) == -1) {
                    numinpt = iptno1;
                    iptno1 = as;
                }

                boolean falg = false;
                boolean asd = false;//少拣标记
                if (numinpt.compareTo(storageNumber)==-1) {//少拣
                    asd = true;
                }

                if (storageStock!=null) {
                    if(storageStock.getStockstate().equals("IN_STOCK")) {
                        BigDecimal snum = new BigDecimal(storageStock.getNum());//库存数
                        if (snum.compareTo(numinpt)==1) {//库存 大于 拣货
                            StorageStockParam param2 = new StorageStockParam();
                            BasicGoods basicGoods = basicGoodsService.getById(storageStock.getGoodsId());
                            BigDecimal u = new BigDecimal(1);
                            if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
                                u = new BigDecimal(basicGoods.getGoodsCharging());
                            }
                            BigDecimal v = new BigDecimal(1);
                            if(ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
                                v = new BigDecimal(basicGoods.getGoodsVolume());
                            }
                            BigDecimal w = new BigDecimal(1);
                            if(ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
                                w = new BigDecimal(basicGoods.getGoodsWeight());
                            }
                            BigDecimal less = snum.subtract(numinpt);
                            storageStock.setNum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            storageStock.setCanNum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            storageStock.setBalenum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            storageStock.setStockWeight(less.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            storageStock.setStockVolume(less.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            storageStock.setStockMoney(less.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            ToolUtil.copyProperties(storageStock,param2);
                            param2.setCreateTime(new Date());
                            param2.setCreateUser(user.getUserId());
                            param2.setUpdateTime(null);
                            param2.setUpdateUser(null);
                            storageStockService.update(param2);//修改 库存信息

                            param2.setStockId(Long.parseLong(param2.getId().toString()));
                            param2.setId(null);
                            param2.setNum(numinpt.toString());
                            param2.setBalenum(numinpt.toString());
                            param2.setCanNum(numinpt.toString());
                            param2.setStockWeight(numinpt.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            param2.setStockVolume(numinpt.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            param2.setStockMoney(numinpt.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            param2.setStockstate("PENDING_SHIPMENT");
                            storageStockService.addReturnId(param2);//生成 待出库库存

                            storageOutDetailed.setStockId(Long.parseLong(param2.getId().toString()));
                        } else if (snum.compareTo(numinpt)==0){//库存 等于 拣货
                            StorageStockParam param2 = new StorageStockParam();
                            param2.setId(storageStock.getId());
                            param2.setStockstate("PENDING_SHIPMENT");
                            storageStockService.update(param2);//修改 库存为待出库
                        } else if (snum.compareTo(numinpt)==-1) {//库存 小于 拣货
                            falg = true;
                        }
                    } else {
                        falg = true;
                    }
                } else {
                    falg = true;
                }
                if (falg) {
                    StorageStockParam stockParam = new StorageStockParam();
                    OutOrderDetailedParam outOrderDetailedParam = new OutOrderDetailedParam();//查询是否指定 批次 出库
                    outOrderDetailedParam.setDetailedOutNumber(outResult2.getOutOrderNumber());
                    outOrderDetailedParam.setDetailedGoodsId(Long.parseLong(storageOutDetailed.getStorageGoodsId().toString()));
                    OutOrderDetailedResult outOrderDetailedResult =  outOrderDetailedService.findBySpec(outOrderDetailedParam);
                    if(outOrderDetailedResult != null) {
                        if(ToolUtil.isNotEmpty(outOrderDetailedResult.getDetailedBatchnumber())   && ToolUtil.isNotEmpty(basicCustomer.getGoodsBatchFlag()) && "Yes".equals(basicCustomer.getGoodsBatchFlag())) {
                            stockParam.setGoodsbatchs(outOrderDetailedResult.getDetailedBatchnumber());
                        }
                    }
                    stockParam.setWarehouseId(outResult2.getOutStorehouseId());
                    stockParam.setGoodsId(Long.parseLong(storageOutDetailed.getStorageGoodsId().toString()));
                    stockParam.setStockstate("IN_STOCK");
                    StorageStockResult rStockResult = storageStockService.sumbysqzkk(stockParam);

                    if (rStockResult!=null) {
                        BigDecimal numrrr = new BigDecimal(rStockResult.getNum());//当前库存
                        if (numinpt.compareTo(numrrr)==1) {
                            data.setSuccess(false);
                            data.setMessage(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                            return data;
                        } else {
                            StorageOutDetailedResult result = new StorageOutDetailedResult();
                            ToolUtil.copyProperties(storageOutDetailed,result);
                            data.setSuccess(true);
                            data.setCode(405);
                            data.setData(this.completeNew(result,user));
                            data.setMessage("Pallet quantity has changed! Reprocessing completed!");
                            return data;
                        }
                    }

                    data.setSuccess(false);
                    data.setMessage(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                    return data;
                }

                if (asd) {
                    BasicGoods basicGoods = basicGoodsService.getById(storageOutDetailed.getStorageGoodsId());
                    BigDecimal u = new BigDecimal(1);
                    if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
                        u = new BigDecimal(basicGoods.getGoodsCharging());
                    }
                    BigDecimal v = new BigDecimal(1);
                    if(ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
                        v = new BigDecimal(basicGoods.getGoodsVolume());
                    }
                    BigDecimal w = new BigDecimal(1);
                    if(ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
                        w = new BigDecimal(basicGoods.getGoodsWeight());
                    }
                    param.setId(storageOutDetailed.getId());
                    param.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
                    param.setStockId(storageOutDetailed.getStockId());
                    param.setStorageOutStatus("PICKED");
                    param.setStorageNumber(numinpt.toString());
                    param.setRealNum(numinpt.toString());
                    param.setStoragePutbalenum(numinpt.toString());//出库数量为拣货单大包数
                    param.setStoragePrice(u.toString());
                    param.setStorageWeight(String.valueOf(numinpt.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总重量
                    param.setStorageVolume(String.valueOf(numinpt.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总体积
                    param.setStorageMoney(String.valueOf(u.multiply(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                    param.setStorageOutUser(user.getUserId());//操作人user
                    param.setStorageOutName(user.getName());//操作人
                    param.setSigns("I");
                    this.update(param);

                    if (asd) {
                        //生成少拣  拣货明细
                        BigDecimal less = storageNumber.subtract(numinpt);
                        StorageOutDetailedParam detailedParam = new StorageOutDetailedParam();
                        detailedParam.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());//拣货单编号
                        detailedParam.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());//商品编号
                        detailedParam.setStorageGoodsId(storageOutDetailed.getStorageGoodsId());
                        detailedParam.setStorageGoodsname(storageOutDetailed.getStorageGoodsname());
                        detailedParam.setStorageNumber(less.toString());//出库数
                        detailedParam.setStoragePutbalenum(less.toString());
                        detailedParam.setStorageBatchnumber(storageOutDetailed.getStorageBatchnumber());
                        detailedParam.setStorageDate(storageOutDetailed.getStorageDate());
                        detailedParam.setStorageOutStatus("PENDING_PICKING");
                        detailedParam.setTrayNumber(r.getTrayNumber());
                        detailedParam.setStoragePostionNumber(r.getStoragePostionNumber());
                        detailedParam.setPickingStatus(DateUtil.getDateS());
                        detailedParam.setCurrentOperator(user.getAccount());
                        detailedParam.setGoodsUnit(storageOutDetailed.getGoodsUnit());
                        detailedParam.setGoodsSpecs(storageOutDetailed.getGoodsSpecs());
                        String a = storageStockService.matching(detailedParam);//匹配库位
                        if ("n".equals(a)) {
                            data.setSuccess(false);
                            data.setMessage(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                            return data;
                        }
                    }
                } else {
                    StorageOutDetailedParam updateDetailedParam = new StorageOutDetailedParam();
                    updateDetailedParam.setId(storageOutDetailed.getId());
                    updateDetailedParam.setStockId(storageOutDetailed.getStockId());
                    updateDetailedParam.setStorageOutStatus("PICKED");
                    updateDetailedParam.setRealNum(numinpt.toString());
                    updateDetailedParam.setStorageOutUser(user.getUserId());//操作人user
                    updateDetailedParam.setStorageOutName(user.getName());//操作人
                    updateDetailedParam.setSigns("J");
                    this.update(updateDetailedParam);

                    StorageOutDetailedParam p = new StorageOutDetailedParam();//校验是否全部拣完
                    p.setStorageOutNumber(r.getStorageOutNumber());
                    p.setStorageOutStatus("PENDING_PICKING");
                    if (ToolUtil.isNotEmpty(outResult2.getMark())) {
                        list = this.findListBymaekSpec(p,outResult2.getMark());
                    } else {
                        list = this.findListBySpec(p);
                    }
                    if (list.size()==0) {
                        List<StorageOutResult> olist = new ArrayList<StorageOutResult>();
                        if (ToolUtil.isNotEmpty(outResult2.getMark())) {
                            StorageOutParam outParam = new StorageOutParam();
                            outParam.setMark(outResult2.getMark());
                            olist = storageOutService.findListByMakeSpec(outParam);
                        } else {
                            olist.add(outResult2);
                        }

                        for(StorageOutResult outResult : olist ) {
                            StorageOutParam outParam = new StorageOutParam();
                            outResult.setOutOrderStatus("PENDING_APPROVAL");

                            ToolUtil.copyProperties(outResult,outParam);
                            storageOutService.update(outParam);
                        }
                    }
                }
            }
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData zhidingpickingmark(StorageStockParam sp, BigDecimal iptno1, StorageOutDetailedParam param3,
                                           User user,String storageOutNumber) {
        String[] strs = storageOutNumber.split(",");
        StorageOutParam outParam = new StorageOutParam();
        outParam.setStorageOutNumber(strs[0]);
        StorageOutResult outResult = storageOutService.findBySpec(outParam);
        StorageOutDetailedParam paramno1 = new StorageOutDetailedParam();
        paramno1.setStorageGoodsId(param3.getStorageGoodsId());
        paramno1.setStoragePostionNumber(param3.getStoragePostionNumber());
        paramno1.setTrayNumber(param3.getTrayNumber());
        paramno1.setStorageBatchnumber(param3.getStorageBatchnumber());
        paramno1.setStorageOutStatus("PENDING_PICKING");

        List<StorageOutDetailedResult> listno1 = this.findListBySpecandstrs(paramno1,strs);
        if(listno1.size()==0) {
            return ResponseData.success();
        }
        StorageOutDetailedResult storageOutDetailedResult = this.sumBySpecandstrs(paramno1,strs);
        if(storageOutDetailedResult == null) {
            return ResponseData.success();
        }
        BigDecimal as = new BigDecimal(0);
        BigDecimal sumnum = new BigDecimal(storageOutDetailedResult.getStorageNumber());//总数
        if (iptno1.compareTo(sumnum)==1) {//拣超
            return ResponseData.error("Over-picking not allowed!");
        }
        if (iptno1.compareTo(as)==0) {
            return ResponseData.error("Cannot be zero!");
        }
        for (StorageOutDetailedResult storageOutDetailed : listno1) {
            if (iptno1.compareTo(as)==0) {
                break;
            }
            List<StorageStockResult> srl2 = storageStockService.finByOnlyOnePositionNumber(sp);
            List<StorageOutDetailedResult> list = new ArrayList<StorageOutDetailedResult>();
            BigDecimal storageNumber = new BigDecimal(storageOutDetailed.getStorageNumber());//明细数量

            BigDecimal numinpt = new BigDecimal(0);//拣货数
            if(iptno1.compareTo(storageNumber) == 1) {
                numinpt = storageNumber;
                iptno1 = iptno1.subtract(storageNumber);
            } else if(iptno1.compareTo(storageNumber) == 0) {
                numinpt = storageNumber;
                iptno1 = as;
            } else if(iptno1.compareTo(storageNumber) == -1) {
                numinpt = iptno1;
                iptno1 = as;
            }

            BigDecimal less2 = storageNumber.subtract(numinpt);
            boolean asd = false;//少拣标记
            if (numinpt.compareTo(storageNumber)==-1) {//少拣
                asd = true;
            }

            boolean breakflag = false;
            StorageOutDetailedParam param = new StorageOutDetailedParam();
            for (StorageStockResult r : srl2) {
                if(breakflag) {
                    break;
                }
                BigDecimal snum = new BigDecimal(r.getNum());//托盘数
                if (snum.compareTo(numinpt)==1) {//库存 大于 拣货
                    StorageStockParam param2 = new StorageStockParam();
                    BasicGoods basicGoods = basicGoodsService.getById(r.getGoodsId());
                    BigDecimal u = new BigDecimal(1);
                    if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
                        u = new BigDecimal(basicGoods.getGoodsCharging());
                    }
                    BigDecimal v = new BigDecimal(1);
                    if(ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
                        v = new BigDecimal(basicGoods.getGoodsVolume());
                    }
                    BigDecimal w = new BigDecimal(1);
                    if(ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
                        w = new BigDecimal(basicGoods.getGoodsWeight());
                    }
                    BigDecimal less = snum.subtract(numinpt);
                    r.setNum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    r.setCanNum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    r.setBalenum(snum.subtract(numinpt).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    r.setStockWeight(less.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    r.setStockVolume(less.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    r.setStockMoney(less.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    ToolUtil.copyProperties(r,param2);
                    param2.setCreateTime(new Date());
                    param2.setCreateUser(user.getUserId());
                    param2.setUpdateTime(null);
                    param2.setUpdateUser(null);
                    storageStockService.update(param2);//修改 库存信息

                    param2.setStockId(Long.parseLong(param2.getId().toString()));
                    param2.setId(null);
                    param2.setNum(numinpt.toString());
                    param2.setBalenum(numinpt.toString());
                    param2.setCanNum(numinpt.toString());
                    param2.setStockWeight(numinpt.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    param2.setStockVolume(numinpt.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    param2.setStockMoney(numinpt.multiply(u).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    param2.setStockstate("PENDING_SHIPMENT");
                    storageStockService.addReturnId(param2);//生成 待出库库存

                    storageOutDetailed.setStorageBatchnumber(param2.getGoodsbatchs());
                    storageOutDetailed.setStorageDate(param2.getProducetime());
                    storageOutDetailed.setTrayNumber(param2.getTrayNumber());
                    storageOutDetailed.setStoragePostionNumber(param2.getPositionNumber());
                    storageOutDetailed.setStoragePrice(param2.getUnitprice());
                    storageOutDetailed.setStorageWeight(param2.getStockWeight());//拣货单总重量
                    storageOutDetailed.setStorageVolume(param2.getStockVolume());//拣货单总体积
                    storageOutDetailed.setStorageMoney(param2.getStockMoney());
                    storageOutDetailed.setStockId(Long.parseLong(param2.getId().toString()));
                    storageOutDetailed.setStorageOutStatus("PICKED");
                    storageOutDetailed.setStoragePutbalenum(numinpt.toString());
                    storageOutDetailed.setStorageNumber(numinpt.toString());
                    storageOutDetailed.setRealNum(numinpt.toString());
                    ToolUtil.copyProperties(storageOutDetailed,param );
                    param.setStorageOutUser(user.getUserId());//操作人user
                    param.setStorageOutName(user.getName());//操作人
                    param.setSigns("V");
                    this.update(param);
                    numinpt = new BigDecimal(0);
                    breakflag = true;
                }
                if (snum.compareTo(numinpt)==0) {//库存 等于 拣货
                    StorageStockParam param2 = new StorageStockParam();
                    param2.setStockstate("PENDING_SHIPMENT");
                    param2.setId(r.getId());
                    storageStockService.update(param2);//修改 库存为待出库
                    storageOutDetailed.setStorageBatchnumber(r.getGoodsbatchs());
                    storageOutDetailed.setStorageDate(r.getProducetime());
                    storageOutDetailed.setTrayNumber(r.getTrayNumber());
                    storageOutDetailed.setStoragePostionNumber(r.getPositionNumber());
                    storageOutDetailed.setStoragePrice(r.getUnitprice());
                    storageOutDetailed.setStorageWeight(r.getStockWeight());//拣货单总重量
                    storageOutDetailed.setStorageVolume(r.getStockVolume());//拣货单总体积
                    storageOutDetailed.setStorageMoney(r.getStockMoney());
                    storageOutDetailed.setStockId(Long.parseLong(r.getId().toString()));
                    storageOutDetailed.setStorageOutStatus("PICKED");
                    storageOutDetailed.setStoragePutbalenum(numinpt.toString());
                    storageOutDetailed.setStorageNumber(numinpt.toString());
                    storageOutDetailed.setRealNum(numinpt.toString());
                    ToolUtil.copyProperties(storageOutDetailed,param);
                    param.setStorageOutUser(user.getUserId());//操作人user
                    param.setStorageOutName(user.getName());//操作人
                    param.setSigns("W");
                    this.update(param);
                    numinpt = new BigDecimal(0);
                    breakflag = true;
                }
                if (snum.compareTo(numinpt)==-1) {//库存 小于 拣货
                    StorageStockParam param2 = new StorageStockParam();
                    param2.setStockstate("PENDING_SHIPMENT");
                    param2.setId(r.getId());
                    storageStockService.update(param2);//修改 库存为待出库

                    StorageOutDetailedParam detailedParam = new StorageOutDetailedParam();
                    detailedParam.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());//拣货单编号
                    detailedParam.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());//商品编号
                    detailedParam.setStorageGoodsId(storageOutDetailed.getStorageGoodsId());
                    detailedParam.setStorageGoodsname(storageOutDetailed.getStorageGoodsname());
                    detailedParam.setStorageNumber(r.getNum());//出库数
                    detailedParam.setRealNum(r.getNum());
                    detailedParam.setStoragePutbalenum(r.getBalenum());
                    detailedParam.setStorageBatchnumber(r.getGoodsbatchs());
                    detailedParam.setStorageDate(r.getProducetime());
                    detailedParam.setRecommendBatchs(r.getPutTime());
                    detailedParam.setStorageOutStatus("PICKED");
                    detailedParam.setTrayNumber(r.getTrayNumber());
                    detailedParam.setStoragePostionNumber(r.getPositionNumber());
                    detailedParam.setStoragePrice(r.getUnitprice());
                    detailedParam.setStorageWeight(r.getStockWeight());//拣货单总重量
                    detailedParam.setStorageVolume(r.getStockVolume());//拣货单总体积
                    detailedParam.setStorageMoney(r.getStockMoney());
                    detailedParam.setStockId(Long.parseLong(r.getId().toString()));//库存id
                    detailedParam.setGoodsUnit(storageOutDetailed.getGoodsUnit());
                    detailedParam.setGoodsSpecs(storageOutDetailed.getGoodsSpecs());
                    detailedParam.setStorageOutUser(user.getUserId());//操作人user
                    detailedParam.setStorageOutName(user.getName());//操作人
                    detailedParam.setSigns("X");
                    this.add(detailedParam);
                    numinpt = numinpt.subtract(snum);
                }
            }
            if (asd) {
                if(numinpt.compareTo(as)==0) {
                    //生成少拣  拣货明细
                    StorageOutDetailedParam detailedParam = new StorageOutDetailedParam();
                    detailedParam.setStorageBatchnumber(storageOutDetailed.getStorageBatchnumber());
                    detailedParam.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());//拣货单编号
                    detailedParam.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());//商品编号
                    detailedParam.setStorageGoodsId(storageOutDetailed.getStorageGoodsId());
                    detailedParam.setStorageGoodsname(storageOutDetailed.getStorageGoodsname());
                    detailedParam.setStorageNumber(less2.toString());//出库数
                    detailedParam.setStoragePutbalenum(less2.toString());
                    detailedParam.setStorageOutStatus("PENDING_PICKING");
                    detailedParam.setStoragePostionNumber(param.getStoragePostionNumber());
                    detailedParam.setGoodsUnit(storageOutDetailed.getGoodsUnit());
                    detailedParam.setGoodsSpecs(storageOutDetailed.getGoodsSpecs());
                    String a = storageStockService.matching(detailedParam);//匹配库位
                    if ("n".equals(a)) {
                        return ResponseData.error(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                    }
                } else {
                    param.setId(storageOutDetailed.getId());
                    this.delete(param);
                    less2 = less2.add(numinpt);
                    //生成少拣  拣货明细
                    StorageOutDetailedParam detailedParam = new StorageOutDetailedParam();
                    detailedParam.setStorageBatchnumber(storageOutDetailed.getStorageBatchnumber());
                    detailedParam.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());//拣货单编号
                    detailedParam.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());//商品编号
                    detailedParam.setStorageGoodsId(storageOutDetailed.getStorageGoodsId());
                    detailedParam.setStorageGoodsname(storageOutDetailed.getStorageGoodsname());
                    detailedParam.setStorageNumber(less2.toString());//出库数
                    detailedParam.setStoragePutbalenum(less2.toString());
                    detailedParam.setStorageOutStatus("PENDING_PICKING");
                    detailedParam.setStoragePostionNumber(sp.getPositionNumber());
                    detailedParam.setGoodsUnit(storageOutDetailed.getGoodsUnit());
                    detailedParam.setGoodsSpecs(storageOutDetailed.getGoodsSpecs());
                    String a = storageStockService.matching(detailedParam);//匹配库位
                    if ("n".equals(a)) {
                        return ResponseData.error(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                    }
                }
            } else {
                param.setId(storageOutDetailed.getId());
                this.delete(param);
                //生成少拣  拣货明细
                StorageOutDetailedParam detailedParam = new StorageOutDetailedParam();
                detailedParam.setStorageBatchnumber(storageOutDetailed.getStorageBatchnumber());
                detailedParam.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());//拣货单编号
                detailedParam.setStorageGoodscode(storageOutDetailed.getStorageGoodscode());//商品编号
                detailedParam.setStorageGoodsId(storageOutDetailed.getStorageGoodsId());
                detailedParam.setStorageGoodsname(storageOutDetailed.getStorageGoodsname());
                detailedParam.setStorageNumber(numinpt.toString());//出库数
                detailedParam.setStoragePutbalenum(numinpt.toString());
                detailedParam.setStorageOutStatus("PENDING_PICKING");
                detailedParam.setStoragePostionNumber(sp.getPositionNumber());
                detailedParam.setGoodsUnit(storageOutDetailed.getGoodsUnit());
                detailedParam.setGoodsSpecs(storageOutDetailed.getGoodsSpecs());
                String a = storageStockService.matching(detailedParam);//匹配库位
                if ("n".equals(a)) {
                    return ResponseData.error(storageOutDetailed.getStorageGoodscode()+"Insufficient system inventory!");
                }
            }
            StorageOutDetailedParam p = new StorageOutDetailedParam();//校验是否全部拣完
            p.setStorageOutNumber(storageOutDetailed.getStorageOutNumber());
            p.setStorageOutStatus("PENDING_PICKING");
            if (ToolUtil.isNotEmpty(outResult.getMark())) {
                list = this.findListBymaekSpec(p,outResult.getMark());
            } else {
                list = this.findListBySpec(p);
            }
            if (list.size()==0) {
                List<StorageOutResult> olist = new ArrayList<StorageOutResult>();
                if (ToolUtil.isNotEmpty(outResult.getMark())) {
                    StorageOutParam outParam2 = new StorageOutParam();
                    outParam2.setMark(outResult.getMark());
                    olist = storageOutService.findListByMakeSpec(outParam2);
                } else {
                    olist.add(outResult);
                }

                for(StorageOutResult outResult2 : olist ) {
                    StorageOutParam outParam2 = new StorageOutParam();
                    outParam2.setId(outResult2.getId());
                    outParam2.setOutOrderStatus("PENDING_APPROVAL");
                    storageOutService.update(outParam2);
                }
            }
        }
        StorageOutDetailedParam param2 = new StorageOutDetailedParam();
        param2.setStorageGoodsId(param3.getStorageGoodsId());
        param2.setStorageOutStatus("PENDING_PICKING");
        List<StorageOutDetailedResult> list = this.findListBySpecandstrs(param2,strs);
        for (StorageOutDetailedResult r : list) {
            this.completeNew(r,user);
        }
        List<StorageOutDetailedResult> sOutDR = new ArrayList<StorageOutDetailedResult>();
        StorageOutDetailedResult result = this.sumBySpecandstrs(param2,strs);
        if(result != null) {
            sOutDR.add(result);
        }
        return ResponseData.success(sOutDR);
    }

    @Override
    public StorageOutDetailedResult sumOutByDay(StorageOutDetailedParam outDetailedParam,Long customerId, Long warehouseId) {
        return this.baseMapper.sumOutByDay(outDetailedParam, customerId,warehouseId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,timeout=200)
    public ResponseData updatePostionNumber(StorageOutDetailedParam param) {
        List<StorageOutDetailedResult> list = new ArrayList<StorageOutDetailedResult>();
        String[] strs = param.getStorageOutNumber().split(",");
        StorageOutParam outParam2 = new StorageOutParam();
        outParam2.setStorageOutNumber(strs[0]);
        StorageOutResult outResult2 = storageOutService.findBySpec(outParam2);
        StorageOutDetailedParam p2 = new StorageOutDetailedParam();//校验是否全部拣完
        p2.setStorageOutNumber(strs[0]);
        p2.setStorageOutStatus("PENDING_PICKING");
        p2.setStorageGoodsId(param.getStorageGoodsId());
        if (ToolUtil.isNotEmpty(outResult2.getMark())) {
            list = this.findListBymaekSpec(p2,outResult2.getMark());
        } else {
            list = this.findListBySpec(p2);
        }

        ResponseData data = new ResponseData();
        List<StorageOutDetailedResult> listss = new ArrayList<StorageOutDetailedResult>();//要推荐库位的  拣货明细
        List<Long> sids =new ArrayList<>();
        for (StorageOutDetailedResult rr : list) {
            sids.add(Long.parseLong(rr.getId().toString()));
            listss.add(rr);
        }
        this.deletebyids(sids);//删除  拣货明细

        List<StorageOutDetailed> solist = new ArrayList<StorageOutDetailed>();//完成库位推荐的   拣货明细
        for (StorageOutDetailedResult rrr : listss) {
            StorageOutDetailedParam detailedParam = new StorageOutDetailedParam();

            ToolUtil.copyProperties(rrr,detailedParam);
            detailedParam.setStorageOutStatus("PENDING_PICKING");
            StorageOutParam outParam = new StorageOutParam();
            outParam.setStorageOutNumber(detailedParam.getStorageOutNumber());
            StorageOutResult outResult = storageOutService.findBySpec(outParam);

            BasicGoods basicGoods = basicGoodsService.getById(detailedParam.getStorageGoodsId());
            BasicCustomer basicCustomer = basicCustomerService.getById(basicGoods.getGoodsCustomerId());
            //出库数量
            BigDecimal outNumber = new BigDecimal(detailedParam.getStorageNumber());
            //单价
            BigDecimal price = new BigDecimal(basicGoods.getGoodsCharging());

            //查询 库存
            //规则：1.如果指定批次，优先出库   2.散托优先出库    3.批次较早，优先出库  4.批次较早的比散托的优先级高
            StorageStockParam sp = new StorageStockParam();//查询 库存单明细 where 商品,仓库id。
            sp.setWarehouseId(outResult.getOutStorehouseId());//仓库id
            sp.setGoodsId(Long.parseLong(basicGoods.getId().toString()));//商品id
            sp.setPositionNumber(param.getUpdateStoragePostionNumber());

            OutOrderDetailedParam outOrderDetailedParam = new OutOrderDetailedParam();
            outOrderDetailedParam.setDetailedOutNumber(outResult.getOutOrderNumber());
            outOrderDetailedParam.setDetailedGoodsId(Long.parseLong(detailedParam.getStorageGoodsId().toString()));
            outOrderDetailedParam.setDetailedBatchnumber(detailedParam.getStorageBatchnumber());
            OutOrderDetailedResult outOrderDetailedResult =  outOrderDetailedService.findBySpec(outOrderDetailedParam);
            if(outOrderDetailedResult == null) {
                outOrderDetailedParam.setDetailedBatchnumber(null);
                outOrderDetailedResult =  outOrderDetailedService.findBySpec(outOrderDetailedParam);
            }
            List<StorageStockResult> srl = new ArrayList<StorageStockResult>();
            if(outOrderDetailedResult != null) {
                if(ToolUtil.isNotEmpty(outOrderDetailedResult.getDetailedBatchnumber())) {
                    //判断是否限制批次出库
                    if(basicCustomer != null && ToolUtil.isNotEmpty(basicCustomer.getGoodsBatchFlag()) && "Yes".equals(basicCustomer.getGoodsBatchFlag())){
                        sp.setGoodsbatchs(detailedParam.getStorageBatchnumber());
                    }

                    srl = storageStockService.finByOnlyOne(sp);
                } else {
                    //判断是否限制批次出库
                    if(basicCustomer != null && ToolUtil.isNotEmpty(basicCustomer.getGoodsBatchFlag()) && "Yes".equals(basicCustomer.getGoodsBatchFlag())){
                        sp.setGoodsbatchs(param.getStorageBatchnumber());
                    }
                    srl = storageStockService.finByOnlyOneOrderByGoodsbatchs(sp);
                }
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
                data.setSuccess(false);
                data.setMessage("No order details found");
                return data;
            }

            int srls = 0;
            BigDecimal result = new BigDecimal(0);
            Long stockId = 0L;
            while (outNumber.compareTo(result) == 1){
                if (srls==srl.size()||srl==null) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
                    data.setSuccess(false);
                    data.setMessage(detailedParam.getStorageGoodscode()+",Insufficient inventory!");
                    return data;
                }
                StorageStockResult stock = srl.get(srls);

                StorageOutDetailed storage = new StorageOutDetailed();
                ToolUtil.copyProperties(detailedParam,storage);
                storage.setSigns("Y");
                storage.setSigns2(null);
                storage.setCreateTime(new Date());
                BigDecimal putNum = new BigDecimal(stock.getNum());//托盘商品数量

                if (putNum.compareTo(result) == 0) {
                    srls++;
                    continue;
                }

                //去除被占用的库存
                if(ToolUtil.isNotEmpty(basicCustomer.getOccupy()) && basicCustomer.getOccupy().equals("Yes")) {
                    StorageOutDetailedParam detailedParam2 = new StorageOutDetailedParam();
                    detailedParam2.setStockId(Long.parseLong(stock.getId().toString()));
                    detailedParam2.setStorageOutStatus("PENDING_PICKING");
                    StorageOutDetailedResult result2 = this.sumByStockId(detailedParam2);
                    if(result2 != null) {
                        BigDecimal rr2 = new BigDecimal(result2.getStorageNumber());
                        if(rr2.compareTo(result) == 1) {
                            if(putNum.compareTo(rr2) == 1) {
                                putNum = putNum.subtract(rr2);
                            } else {
                                srls++;
                                continue;
                            }
                        }
                    }
                } else if (ToolUtil.isNotEmpty(outResult.getMark())){//如果合单  去除被合单占用的库存
                    StorageOutDetailedParam detailedParam2 = new StorageOutDetailedParam();
                    detailedParam2.setStockId(Long.parseLong(stock.getId().toString()));
                    detailedParam2.setStorageOutStatus("PENDING_PICKING");
                    StorageOutDetailedResult result2 = this.sumByStockId2(detailedParam2,outResult.getMark());
                    if(result2 != null) {
                        BigDecimal rr2 = new BigDecimal(result2.getStorageNumber());
                        if(rr2.compareTo(result) == 1) {
                            if(putNum.compareTo(rr2) == 1) {
                                putNum = putNum.subtract(rr2);
                            } else {
                                srls++;
                                continue;
                            }
                        }
                    }
                } else {//去除被同一单子占用的库存
                    StorageOutDetailedParam detailedParam2 = new StorageOutDetailedParam();
                    detailedParam2.setStockId(Long.parseLong(stock.getId().toString()));
                    detailedParam2.setStorageOutStatus("PENDING_PICKING");
                    detailedParam2.setStorageOutNumber(detailedParam.getStorageOutNumber());
                    StorageOutDetailedResult result2 = this.sumByStockId(detailedParam2);
                    if(result2 != null) {
                        BigDecimal rr2 = new BigDecimal(result2.getStorageNumber());
                        if(rr2.compareTo(result) == 1) {
                            if(putNum.compareTo(rr2) == 1) {
                                putNum = putNum.subtract(rr2);
                            } else {
                                srls++;
                                continue;
                            }
                        }
                    }
                }

                //这一托可以出完
                if(outNumber.subtract(putNum).compareTo(result) == -1||outNumber.subtract(putNum).compareTo(result)== 0){
                    //重量
                    BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
                    //体积
                    BigDecimal v = new BigDecimal(basicGoods.getGoodsVolume());
                    if (putNum.subtract(outNumber).compareTo(result) == 1){
                        stockId = Long.parseLong(stock.getId().toString());//库存id

                        //完善 拣货单明细
                        storage.setTrayNumber(stock.getTrayNumber());//托盘编号
                        storage.setStorageBatchnumber(stock.getGoodsbatchs());//批次
                        storage.setStorageDate(stock.getProducetime());//生产日期
                        storage.setRecommendBatchs(stock.getPutTime());//推荐批次
                        storage.setStoragePostionNumber(stock.getPositionNumber());//库位编码
                        storage.setStorageNumber(String.valueOf(outNumber));//出库数量为拣货单数量
                        storage.setStoragePutbalenum(String.valueOf(outNumber));//出库数量为拣货单大包数
                        storage.setStorageWeight(String.valueOf(outNumber.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总重量
                        storage.setStorageVolume(String.valueOf(outNumber.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总体积
                        storage.setStorageMoney(String.valueOf(price.multiply(outNumber).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                        storage.setStockId(stockId);//库存id
                        solist.add(storage);

                        BigDecimal outNumber1	=  outNumber.subtract(putNum).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                        outNumber = outNumber1;//减 出库数量
                        srls++;
                    } else {
                        stockId = Long.parseLong(stock.getId().toString());//库存id
                        //完善 拣货单明细
                        storage.setTrayNumber(stock.getTrayNumber());//托盘编号
                        storage.setStorageBatchnumber(stock.getGoodsbatchs());//批次
                        storage.setStorageDate(stock.getProducetime());//生产日期
                        storage.setRecommendBatchs(stock.getPutTime());//推荐批次
                        storage.setStoragePostionNumber(stock.getPositionNumber());//库位编码
                        storage.setStorageNumber(String.valueOf(outNumber));//出库数量为拣货单数量
                        storage.setStoragePutbalenum(String.valueOf(outNumber));//出库数量为拣货单大包数
                        storage.setStorageWeight(String.valueOf(outNumber.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总重量
                        storage.setStorageVolume(String.valueOf(outNumber.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));//拣货单总体积
                        storage.setStorageMoney(String.valueOf(price.multiply(outNumber).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                        storage.setStockId(stockId);//库存id
                        solist.add(storage);

                        BigDecimal outNumber1	=  outNumber.subtract(putNum).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                        outNumber = outNumber1;//减 出库数量
                        srls++;
                    }
                }else{
                    //重量
                    BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
                    //体积
                    BigDecimal v = new BigDecimal(basicGoods.getGoodsVolume());
                    //这托出不完
                    stockId = Long.parseLong(stock.getId().toString());//库存id

                    BigDecimal outNumber1	=  outNumber.subtract(putNum).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                    outNumber = outNumber1;//减 出库数量

                    //完善 拣货单明细
                    storage.setTrayNumber(stock.getTrayNumber());
                    storage.setStorageBatchnumber(stock.getGoodsbatchs());//批次
                    storage.setStorageDate(stock.getProducetime());//生产日期
                    storage.setRecommendBatchs(stock.getPutTime());//推荐批次
                    storage.setStoragePostionNumber(stock.getPositionNumber());
                    storage.setStoragePutbalenum(putNum.toString());
                    storage.setStorageNumber(putNum.toString());
                    storage.setStorageWeight(String.valueOf(putNum.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                    storage.setStorageVolume(String.valueOf(putNum.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                    storage.setStorageMoney(String.valueOf(price.multiply(putNum).setScale(3, BigDecimal.ROUND_HALF_DOWN)));
                    storage.setStockId(stockId);//库存id
                    solist.add(storage);
                    srls++;
                }
                stock = new StorageStockResult();
                storage = new StorageOutDetailed();
            }
            if (solist.size() > 0) {
                for (StorageOutDetailed addp : solist) {
                    if(addp.getStoragePostionNumber().equals(param.getUpdateStoragePostionNumber()) && addp.getStorageBatchnumber().equals(param.getStorageBatchnumber())) {
                        addp.setSigns2("Forward");
                    }
                }
                this.saveBatchList(solist);
            }
            solist = new ArrayList<StorageOutDetailed>();
        }

        //合并同一托拣货明细
        String[] strs2 = param.getStorageOutNumber().split(",");
        StorageOutDetailedParam storageOutDetailedParamp = new StorageOutDetailedParam();
        List<StorageOutDetailedResult>  list2 =  this.findgoodsbystrs(storageOutDetailedParamp,strs2);//合计明细  按库存id和订单号去重
        for (StorageOutDetailedResult r2 : list2) {
            if(r2.getStockId()==null) {
                continue;
            }
            StorageOutDetailedParam param2 = new StorageOutDetailedParam();
            param2.setStockId(r2.getStockId());
            param2.setStorageOutNumber(r2.getStorageOutNumber());
            param2.setStorageGoodsId(r2.getStorageGoodsId());
            List<StorageOutDetailedResult>  list3 =  this.findListStockIdBySpec(param2);//查询是否存在多条
            if(list3.size()>=2) {
                for (int i = 0; i < list3.size(); i++) {
                    StorageOutDetailedParam storageOutDetailedParam3 = new StorageOutDetailedParam();
                    storageOutDetailedParam3.setId(list3.get(i).getId());
                    this.delete(storageOutDetailedParam3);
                }
                StorageOutDetailedParam storageOutDetailedParam2 = new StorageOutDetailedParam();
                StorageOutDetailedResult storageOutDetailedResult = list3.get(0);
                storageOutDetailedResult.setId(null);
                storageOutDetailedResult.setStoragePutbalenum(r2.getStoragePutbalenum());
                storageOutDetailedResult.setStorageNumber(r2.getStorageNumber());
                storageOutDetailedResult.setStorageWeight(r2.getStorageWeight());
                storageOutDetailedResult.setStorageVolume(r2.getStorageVolume());
                storageOutDetailedResult.setStorageMoney(r2.getStorageMoney());
                ToolUtil.copyProperties(storageOutDetailedResult, storageOutDetailedParam2);
                storageOutDetailedParam2.setSigns("Z");
                this.add(storageOutDetailedParam2);
            }
        }

        StorageOutDetailedParam p = new StorageOutDetailedParam();//校验是否全部拣完
        p.setStorageOutNumber(outResult2.getStorageOutNumber());
        p.setStorageOutStatus("PENDING_PICKING");
        p.setStorageGoodsId(param.getStorageGoodsId());
        if (ToolUtil.isNotEmpty(outResult2.getMark())) {
            list = this.findListBymaekSpec(p,outResult2.getMark());
        } else {
            list = this.findListBySpec(p);
        }
        if(list.size()>0) {
            return ResponseData.success(list);
        }
        return ResponseData.success();
    }

    @Override
    public Workbook exportToExcel(StorageOutDetailedParam storageOutDetailedParam) {
        //这是表头
        String[] arr = {"Picking Status","Picking Staff","Picking Time","Picking Order Number","Location","Tray Number","Product"
                ,"ID","Item Code","Outbound Quantity","Actual Quantity","Inbound Batch","Production Date","Batch"
                ,"Weight","Volume","Unit Price","Amount","Category"
                ,"Unit","Package","Specification","Remarks"};
        //这是具体数据
        List<List<String>> list = new ArrayList<>();

        User user = userService.getcurrentUser();
        storageOutDetailedParam.setDeptId(user.getDeptId());
        List<StorageOutDetailedResult> datalist = this.findExportToExcelBySpec(storageOutDetailedParam,user.getOperateId());
        for (StorageOutDetailedResult r : datalist) {
            List<String> list1 = new ArrayList<String>();
            list1.add(r.getStorageOutStatus());
            list1.add(r.getStorageOutName());
            list1.add(r.getUpdateTimestr());
            list1.add(r.getStorageOutNumber());
            list1.add(r.getStoragePostionNumber());
            list1.add(r.getTrayNumber());
            list1.add(r.getStorageGoodsname());
            list1.add(r.getStorageGoodscode());
            list1.add(r.getGoodsNo());
            list1.add(r.getStorageNumber());
            list1.add(r.getRealNum());
            list1.add(r.getRecommendBatchs());
            list1.add(r.getStorageDate());
            list1.add(r.getStorageBatchnumber());
            list1.add(r.getStorageWeight());
            list1.add(r.getStorageVolume());
            list1.add(r.getStoragePrice());
            list1.add(r.getStorageMoney());
            list1.add(r.getStorageLargeclass());
            list1.add(r.getGoodsUnit());
            list1.add(r.getStoragePacking());
            list1.add(r.getGoodsSpecs());
            list1.add(r.getNotes());
            list.add(list1);
        }
        return ExcelWrite.writeToExcelByList(arr, list);
    }


    @Override
    @Transactional(rollbackFor = Exception.class,timeout=60)
    public ResponseData checkJhwStock(OutOrder outOrder,ResponseData data) {
        //新增拣货单明细
        OutOrderDetailedParam outOrderDetailedParam = new OutOrderDetailedParam();
        outOrderDetailedParam.setDetailedOutNumber(outOrder.getOutOrderNumber());
        List<OutOrderDetailedResult> orderDetailedResultList = outOrderDetailedService.findListBySpec(outOrderDetailedParam);
        for (OutOrderDetailedResult orderDetailedResult : orderDetailedResultList){
            StorageStockParam stockParam = new StorageStockParam();
            stockParam.setWarehouseId(outOrder.getOutStorehouseId());
            stockParam.setGoodsId(orderDetailedResult.getDetailedGoodsId());
            stockParam.setGoodsbatchs(orderDetailedResult.getDetailedBatchnumber());
            stockParam.setStockstate("IN_STOCK");
            StorageStockResult rStockResult = storageStockService.sumbyJhwsqzkk(stockParam);//查询该品 库存

            StorageOutDetailedParam detailedParam  = new StorageOutDetailedParam();
            detailedParam.setStorageGoodsId(orderDetailedResult.getDetailedGoodsId());
            detailedParam.setStorageBatchnumber(orderDetailedResult.getDetailedBatchnumber());
            StorageOutDetailedResult  StorageOutDetailedResult = findSumBySpec(detailedParam, outOrder.getOutStorehouseId());//查询待拣货 库存
            BigDecimal onum = new BigDecimal(0);//待拣库存
            if (StorageOutDetailedResult!=null) {
                onum = new BigDecimal(StorageOutDetailedResult.getStorageNumber());
            }
            BigDecimal Num = new BigDecimal(orderDetailedResult.getDetailedNumber()).add(onum);//出库库存
            if (rStockResult!=null) {
                BigDecimal num = new BigDecimal(rStockResult.getNum());
                if (Num.compareTo(num)==1) {
                    BigDecimal sub = Num.subtract(num);//系统库存   -   待拣库存 =  实际库存
                    data.setSuccess(false);
                    data.setMessage("Insufficient inventory at picking location！"+outOrder.getCustomerNumber()+"<br>Commodity Code:"+orderDetailedResult.getDetailedGoodscode()+"<br>Commodity Name:"+orderDetailedResult.getDetailedGoodsname()+"<br>Shortage:"+sub.toString()+"units<br>Batch No.:"+orderDetailedResult.getDetailedBatchnumber());
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return data;
                }
            } else {
                data.setSuccess(false);
                data.setMessage("Insufficient inventory at picking location！"+outOrder.getCustomerNumber()+"<br>Commodity Code:"+orderDetailedResult.getDetailedGoodscode()+"<br>Commodity Name:"+orderDetailedResult.getDetailedGoodsname()+"<br>Shortage:"+Num.toString()+"units<br>Batch No.:"+orderDetailedResult.getDetailedBatchnumber());
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return data;
            }
        }
        //更新出库订单
        outOrder.setOutOrderStatus("PENDING_CONFIRMATION");
        OutOrderParam param = new OutOrderParam();
        ToolUtil.copyProperties(outOrder,param);
        outOrderService.update(param);
        return ResponseData.success();
    }


    @Override
    @Transactional(rollbackFor = Exception.class,timeout=60)
    public ResponseData outSingleWhHSHN(OutOrder outOrder,ResponseData data) {

        StorageOutParam storageOutParam = new StorageOutParam();
        storageOutParam.setStorageOutNumber(outOrder.getCustomerNumber());
        //出库订单编号
        storageOutParam.setOutOrderNumber(outOrder.getOutOrderNumber());
        //库房id
        storageOutParam.setOutStorehouseId(outOrder.getOutStorehouseId());
        //库房名称
        storageOutParam.setStorehouseName(outOrder.getStorehouseName());
        //收货地址
        storageOutParam.setGetLocation(outOrder.getGetLocation());
        //收货地址id
        storageOutParam.setGetLocationId(outOrder.getGetLocationId());
        //收货单位
        storageOutParam.setGetUnit(outOrder.getGetUnit());
        //收货单位id
        storageOutParam.setGetUnitId(outOrder.getGetUnitId());
        //交付id
        storageOutParam.setDeliverId(outOrder.getDeliverId());
        storageOutParam.setDeptId(outOrder.getDeptId());
        storageOutParam.setOutOrderStatus("INCOMPLETE");

        storageOutParam.setSetMentiontime(outOrder.getSetMentiontime());//发货时间
        //不重复
        String uuidOne = UUID.randomUUID().toString().replaceAll("-","");
        storageOutParam.setRepetition(uuidOne);

        storageOutService.add(storageOutParam);

        //新增拣货单明细
        OutOrderDetailedParam outOrderDetailedParam = new OutOrderDetailedParam();
        outOrderDetailedParam.setDetailedOutNumber(outOrder.getOutOrderNumber());
        List<OutOrderDetailedResult> orderDetailedResultList = outOrderDetailedService.findListBySpec(outOrderDetailedParam);

        BasicCustomer basicCustomer = basicCustomerService.getById(outOrder.getOutCustomerId());


        List<StorageOutDetailed> solist2 = new ArrayList<StorageOutDetailed>();//要保存的 拣货单明细list
        for (OutOrderDetailedResult orderDetailedResult : orderDetailedResultList){
            StorageOutDetailed storage = prepareViewData(orderDetailedResult);
            storage.setSigns("A");
            storage.setStorageOutNumber(storageOutParam.getStorageOutNumber());//出库单
            storage.setStoragePutbalenum(orderDetailedResult.getDetailedNumber());//大包数
            storage.setStorageNumber(orderDetailedResult.getDetailedNumber());//数量

            StorageStockParam stockParam = new StorageStockParam();
            stockParam.setWarehouseId(outOrder.getOutStorehouseId());
            stockParam.setGoodsId(Long.parseLong(storage.getStorageGoodsId().toString()));
            //判断是否限制批次出库
            if(basicCustomer != null && ToolUtil.isNotEmpty(basicCustomer.getGoodsBatchFlag()) && "Yes".equals(basicCustomer.getGoodsBatchFlag())){
                stockParam.setGoodsbatchs(orderDetailedResult.getDetailedBatchnumber());
            }

            stockParam.setStockstate("IN_STOCK");
            BigDecimal onum = new BigDecimal(0);//待拣库存
            StorageOutDetailedParam detailedParam  = new StorageOutDetailedParam();
            detailedParam.setStorageGoodsId(Long.parseLong(storage.getStorageGoodsId().toString()));
            detailedParam.setStorageBatchnumber(orderDetailedResult.getDetailedBatchnumber());
            StorageOutDetailedResult  StorageOutDetailedResult = findSumBySpec(detailedParam, outOrder.getOutStorehouseId());//查询待拣货 库存
            if (StorageOutDetailedResult!=null) {
                onum = new BigDecimal(StorageOutDetailedResult.getStorageNumber());
            }
            BigDecimal Num = new BigDecimal(storage.getStorageNumber()).add(onum);//系统库存
            //全部库存是否充足
            solist2.add(storage);
        }



        this.saveBatchList(solist2);
        //更新出库订单
        outOrder.setOutOrderStatus("PENDING_SHIPMENT");
        OutOrderParam param = new OutOrderParam();
        ToolUtil.copyProperties(outOrder,param);
        outOrderService.update(param);
        data.setSuccess(true);
        data.setCode(200);
        data.setMessage("Outbound successful！"+outOrder.getCustomerNumber());
        data.setData(orderDetailedResultList);

        return data;
    }


    /**
     * 出库单【分配商品到快递单】
     * @param param
     * @param num
     * @return
     */
    @Override
    public ResponseData allocateGoods(StorageOutDetailedParam param, String num) {
        User user = userAop.getUser();
        ResponseData data = new ResponseData();
        //本次拣货检出的商品
        StorageOutDetailed  storageOutDetailed = this.getById(param.getId());//拣货明细
        String trayNumber = storageOutDetailed.getTrayNumber();
        String storageNumber = storageOutDetailed.getStorageNumber();//数量
        String storagePostionNumber = storageOutDetailed.getStoragePostionNumber();
        String storageOutNumber = storageOutDetailed.getStorageOutNumber();
        Long storageGoodsId = storageOutDetailed.getStorageGoodsId();

        //本单包含本商品的快递单
        List<WhOutOrderAllocateResult> whOutOrderAllocateResultAll = outOrderAllocateService.getOutOrderAllocate(storageOutNumber,storageGoodsId);

        //本单已分配本商品的快递单
        WhOutOrderAllocateResult whOutOrderAllocateResultUn = outOrderAllocateService.getOutOrderAllocateUn(storageOutNumber,storageGoodsId);

        Map<String,Object> map = new HashMap<>();
        int dfp = 0;
        for (WhOutOrderAllocateResult whOutOrderAllocateResult : whOutOrderAllocateResultAll) {
            String expressDelivery = whOutOrderAllocateResult.getExpressDelivery();
            // 需要分配的数量
            String detailedNumber = whOutOrderAllocateResult.getDetailedNumber();
            String detailedNumber1 = "0";
            // 已分配的数量
            if(ToolUtil.isNotEmpty(whOutOrderAllocateResultUn) ){

                detailedNumber1 = whOutOrderAllocateResultUn.getDetailedNumber();;
            }else{
                detailedNumber1 = "0";
            }
            if(Integer.valueOf(detailedNumber1)<Integer.valueOf(detailedNumber)){
                //待分配数量
                int i = Integer.valueOf(detailedNumber) - Integer.valueOf(detailedNumber1);
                dfp += i;
                map.put(expressDelivery,i);
                if(dfp>Double.valueOf(storageNumber)){
                    break;
                }
            }
        }
        map.forEach((k,v)->{
            WhOutOrderAllocateParam whOutOrderAllocateParam = new WhOutOrderAllocateParam();
            whOutOrderAllocateParam.setExpressDelivery(k);
            whOutOrderAllocateParam.setDetailedNumber(v.toString());
            whOutOrderAllocateParam.setTrayNumber(trayNumber);
            whOutOrderAllocateParam.setStoragePostionNumber(storagePostionNumber);
            whOutOrderAllocateParam.setDeptId(user.getDeptId());
            whOutOrderAllocateParam.setOutOrderNumber(storageOutNumber);
            whOutOrderAllocateParam.setDetailedGoodsId(storageGoodsId);
            whOutOrderAllocateParam.setOutCustomerId(147L);
            whOutOrderAllocateParam.setOutStorehouseId(122L);
            outOrderAllocateService.add(whOutOrderAllocateParam);
        });
        return ResponseData.success();
    }

    @Override
    public List<StorageOutDetailedResult> tojianhuo(String goodNo, String storageOutNumber) {
        return this.baseMapper.tojianhuo(goodNo, storageOutNumber);
    }

    @Override
    public List<String> getkddByOrder(String storageOutNumber) {
        return this.baseMapper.getkddByOrder(storageOutNumber);
    }

    @Override
    public List<StorageOutDetailedResult> findByStorageOutNumbersPageNew2(String[] strs) {
        return this.baseMapper.findByStorageOutNumbersPageNew2(strs);
    }

    public static void main(String[] args) {
        String str = "拣货位南京城内的就CDC";
        System.out.println(str.contains("CDC"));
    }
}
